Encapsulated js judgment operating system and browser code sharing _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the encapsulated js judgment operating system and browser code sharing, which is very helpful for system and browser compatibility. For more information, see Abstract:
The most important task for front-end development is compatibility, system compatibility, browser compatibility, and so on. Today, I will share a method for judging operating systems and browsers encapsulated 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)  };}());

To determine whether the system is iPad, you only need to determine 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 browser isFirefox:/firefox /. test (UserAgent), // Firefox isOpera:/opera /. test (UserAgent), // operabrowser isSafire:/safari /. test (UserAgent )&&! /Chrome /. test (UserAgent), // safire browser is360:/360se /. test (UserAgent), // 360 browser 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), // IE 7 isIE8:/msie 8.0 /. test (UserAgent), // IE8 isIE9:/msie 9.0 /. test (UserAgent), // ie 9 isIE10:/msie 10.0 /. test (UserAgent), // IE10 isIE11:/msie 11.0 /. test (UserAgent), // IE11 isLB:/lbbrowser /. test (UserAgent), // isWX of the cheetah Browser:/micromessenger /. test (UserAgent), // built-in browser isQQ:/qqbrowser /. test (UserAgent) // QQ browser };}());

]

Summary:
Browsers are tested by myself. The problem may be caused by chrome. Most browsers use the WebKit kernel, so I cut chrome navigator out to distinguish it. If the location of chrome navigator Information or the length after chrome is changed in the future, it is easy to see problems. However, it can be seen at present.

Now, because the mobile UC browser often blocks Baidu's advertisements but does not block google advertisements, we can join in to determine whether it is a UC browser. If it is not, Baidu ads will be displayed, and google ads will be displayed.

If (navigator. userAgent. indexOf ('ucbrowser ')>-1) {alert ("uc Browser");} else {// operations not performed by uc browser}

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

JS to get browser Information
Browser code name: navigator. appCodeName
Browser name: navigator. appName
Browser version: navigator. appVersion
Java support: navigator. javaEnabled ()
MIME Type (array): navigator. mimeTypes
System platform: navigator. platform
Plug-in (array): navigator. plugins
User Agent: navigator. userAgent

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.