Encapsulated JS to judge the operating system and browser code sharing _JAVASCRIPT skills

Source: Internet
Author: User

Summary:
For front-end development, our most important work is compatibility, system compatibility, browser compatibility and so on. Today I'm going to share a method for judging the operating system and the browser that I encapsulate in my project.

Operating system:

var os = (function () {
  var useragent = Navigator.userAgent.toLowerCase ();
  return {
    Isipad     :/ipad/.test (useragent),
    isiphone    :/iphone os/.test (useragent),
    isandroid    :/android/.test (useragent),
    iswindowsce   :/windows ce/.test (useragent),
    iswindowsmobile:/ Windows Mobile/.test (useragent),
    iswin2k     :/windows nt 5.0/.test (useragent),
    isxp      :/windows nt 5.1/.test (useragent),
    isvista     :/windows nt 6.0/.test (useragent),
    isWin7     :/windows nt 6.1/.test (useragent),
    isWin8     :/windows nt 6.2/.test (useragent),
    isWin81     :/windows nt 6.3/.test ( useragent)
  };
} ());

If you want to determine whether the system is an ipad, just judge if (Os.isipad) {}.

Browser:

var bw = (function () {var useragent = navigator.userAgent.toLowerCase (); return {isuc:/ucweb/.test (useragent),//UC Browser Ischrome:/chrome/.test (Useragent.substr ( -33,6)),//Chrome Browse Device Isfirefox:/firefox/.test (useragent),//Firefox browser Isopera:/opera/.test (useragent),//Opera browser Issafire:/ Safari/.test (useragent) &&!/chrome/.test (useragent),//Safire browser is360:/360se/.test (useragent),//360 view Isbaidu:/bidubrowser/.test (useragent),//Baidu browser Issougou:/metasr/.test (useragent),//Sogou browser isIE6:/ MSIE 6.0/.test (useragent),//IE6 isIE7:/msie 7.0/.test (useragent),//IE7 isIE8:/msie 8.0/.test (useragent  ),//IE8 isIE9:/msie 9.0/.test (useragent),//IE9 isIE10:/msie 10.0/.test (useragent),//IE10 isIE11 :/msie 11.0/.test (useragent),//IE11 ISLB:/lbbrowser/.test (useragent),//Cheetah browser ISWX:/micromessenger/.t EST (useragent),//micro-letter built-in browser isqq:/qqbrowser/.test (useragent) QQ Browser}; }());

]

Summary:
The browser is tested by myself, and there may be a problem with the Chrome browser, because most browsers use the WebKit kernel, so I cut out the Chrome navigator to differentiate it. If the future of Chrome's navigator information location or the length of chrome after the change is likely to be a problem, but at the moment it is OK.

Now because the cell phone UC browser often screen Baidu ads, but Google ads do not shield, we can join to determine whether the UC browser, not on the display of Baidu ads, is the display of Google Ads

if (navigator.userAgent.indexOf (' Ucbrowser ') >-1) {
alert ("UC Browser");
} else{
//Is not an operation performed by the UC browser
}

In fact, specific browser some special operations can be done through the

JS Get browser information
Browser code Name: Navigator.appcodename
Browser name: Navigator.appname
Browser version number: Navigator.appversion
Support for Java: navigator.javaenabled ()
MIME type (array): Navigator.mimetypes
System platform: Navigator.platform
Plugins (arrays): Navigator.plugins
User agent: Navigator.useragent

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.