JS judges the browser type and obtains the browser version number.

Source: Internet
Author: User

In web development, browser compatibility is often overwhelmed. This article mainly uses js to encapsulate a method to obtain the browser type and version number of the current client. As we all know, we often judge by analyzing the browser's ueragent, but the ueragent attributes are smelly and long. How can we get the unique features of each browser from this, first, we will analyze the ueragent strings of various browsers: (not completely written, but it is a very important part of the string)

IE (MSIE is followed by the version number)

 

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)


Firefox (Firefox/later versions)


Mozilla/5.0 (windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1

Mozilla/5.0 (windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3

Mozilla/5.0 (windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12


Opera (the version number is later than opera)


Opera/9.27 (Windows NT 5.2; U; ZH-CN)

Opera/8.0 (Macintosh; PPC Mac OS X; U; en)

Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) opera 8.0


Safri (version later)


Mozilla/5.0 (windows; U; Windows NT 5.2) applewebkit/525.13 (khtml, like gecko) version/3.1
Safari/525.13

Mozilla/5.0 (iPhone; U; CPU like Mac OS X) applewebkit/420.1 (khtml, like gecko) version/3.0
Mobile/4a93 Safari/419.3


Chrome (Chrome is followed by the version number)


Mozilla/5.0 (windows; U; Windows NT 5.2) applewebkit/525.13 (khtml, like gecko) Chrome/0.2.149.27 Safari/525.13

Interestingly, Chrome's useragent string also contains the features of safari.


We can construct our objects based on the above features:


VaR UA = navigator. useragent. tolowercase (); <br/> var S = NULL; <br/> var browser = {<br/> MSIE :( S = UA. match (/MSIE/S * ([/D/.] + )/))? S [1]: false, <br/> Firefox :( S = UA. Match (/Firefox // ([/D/.] + )/))? S [1]: false, <br/> chrome :( S = UA. Match (/chrome // ([/D/.] + )/))? S [1]: false, <br/> opera :( S = UA. Match (/opera. ([/D/.] + )/))? S [1]: false, <br/> safari :( S = UA. Match (/varsion // ([/D/.] +). * Safari /))? S [1]: false <br/> }; 

The keyword value is false or the version number of the current browser. Therefore, we can use this object for browser detection.


 

 

 

 

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.