Share the encapsulated js judgment operating system with the browser code and encapsulate the js browser code

Source: Internet
Author: User

Share the encapsulated js judgment operating system with the browser code and encapsulate the js browser code

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:

Copy codeThe Code is as follows:
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:
 
Copy codeThe Code is as follows:
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), // 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/. 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.

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.