JS to determine the current page with what browser open method _javascript tips

Source: Internet
Author: User

Recently do a lot of HTML5 projects, many pages will be through micro-credit microblogging and other SNS to share out. Provide a download of the company app on the sharing page. However, in many applications of the browser, click on the download link can not download the application. So for these browsers we need to give users tips from safari or the system from the browser to open the sharing page, through JS can determine the current page is in what browser open.

The following is a sample code, which shows how to determine whether to open in a micro-letter browser, whether in the QQ space browser or not on Sina Weibo. Of course you can do a little better, plus the judgment is on the mobile device Open or in the PC-side browser open , which can be referred to this article, a bit more subdivision, can be judged in the Android browser Open or Open with the iOS system browser .

if (browser.versions.mobile) {//Determine if the mobile device is open. Browser code below
    var ua = Navigator.userAgent.toLowerCase ();//Get the object to be judged
    if (ua.match (/micromessenger/i) = = " Micromessenger ") {
        //open in micro-letter
    }
    if (Ua.match (/weibo/i) = =" WeiBo ") {
        //on Sina Weibo client open
    }
    if ( Ua.match (/qq/i) = = "QQ") {
        //in QQ space open
    } if
    (Browser.versions.ios) {
        //whether in the iOS browser open
    } 
    if ( browser.versions.android) {
        //whether to open in Android browser}
else {
    //Otherwise it is PC browser open
}

Attach the browser code and use the following methods to determine many browsers. Including the judge IE browser, Opera browser, Apple browser, Google browser, Firefox browser and so on.

var browser = {
  versions:function () {
    var u = navigator.useragent, app = navigator.appversion;
    return {     //Mobile terminal browser version information
      trident:u.indexof (' Trident ') >-1,//ie kernel
      presto:u.indexof (' Presto ') >-1,/ /opera kernel
      webkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernel
      gecko:u.indexof (' Gecko ') >-1 && U.indexof (' khtml ') = = 1,//Firefox kernel
      Mobile:!! U.match (/applewebkit.*mobile.*/),//Whether for mobile terminal
      iOS:!! U.match (/\ (i[^;] +;( U;)? Cpu.+mac OS x/),//ios terminal
      android:u.indexof (' Android ') >-1 | | u.indexof (' Linux ') >-1,//android terminal or UC browser 
   
    iphone:u.indexof (' IPhone ') >-1,//whether for IPhone or Qqhd browser
      ipad:u.indexof (' ipad ') >-1,//whether the IPad
      webApp: U.indexof (' Safari ') = = 1/Whether the Web should program, without head and bottom
    };
  } (),
  language: (navigator.browserlanguage | | navigator.language). toLowerCase ()
}
   

Another method:

With JS to judge, after the search data finally realized the effect, directly on the code

function Is_weixn () { 
  var ua = Navigator.userAgent.toLowerCase (); 
  if (Ua.match (/micromessenger/i) = = "Micromessenger") {return 
    true; 
  } else {return 
    false 
  ; 
}} 

Through the test completely through, whether it is Android or Iphone,ipad can, of course, we in addition to using JS to judge, in other languages to judge is simpler, such as PHP

function Is_weixin () { 
  if (Strpos ($_server[' http_user_agent '], ' Micromessenger ')!== false) {return 
      true;< c12/>} return  
  false; 

The above is for you to share JS to determine the current page with what browser open method, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.