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