JS Common judgment of Mobile end or PC-side or Android and Apple Browser method summary

Source: Internet
Author: User
Tags end regular expression version versions

A long time ago I wrote a browser to determine the browser version or phone type through the browser navigator, which describes the use of navigator to determine the browser type. Make a supplement on this article today!

Js judges the indexOf mode of Android or ios

/ / Determine the access terminal
Var browser={
    Version: function(){
        Var u = navigator.userAgent, app = navigator.appVersion;
        Return {
            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 it is a 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 it is iPhone or QQHD browser
            iPad: u.indexOf('iPad') > -1, //Is it iPad?
            webApp: u.indexOf('Safari') == -1, //whether the web should be the program, no head and bottom
            Weixin: u.indexOf('MicroMessenger') > -1, //Whether WeChat (new in 2015-01-22)
            Qq: u.match(/\sQQ/i) == "qq" //whether qq
        };
    }(),
    Language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
Instructions:


/ / Determine whether the IE kernel
If(browser.versions.trident){ alert("is IE"); }
/ / Determine whether the webKit kernel
If(browser.versions.webKit){ alert("is webKit"); }
/ / Determine whether the mobile side
If(browser.versions.mobile||browser.versions.android||browser.versions.ios){ alert("mobile"); }


Js determines the regular expression mode of Android or ios


If (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
     //alert(navigator.userAgent);
    //Apple
} else if (/(Android)/i.test(navigator.userAgent)) {
     //alert(navigator.userAgent);
     //Android
} else {
    //pc end
};


Related Article

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.