JS to determine whether to open in the micro-mail Browser simple example (recommended) _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 bit better, plus the judgment is on the mobile device open or on the PC side of the browser open, more subdivision, can be judged in Android browser Open or iOS system browser open.

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 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 ()
}
   

This JS to determine whether the micro-letter browser open a simple example (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.