Using JS to identify each version of the browser

Source: Internet
Author: User

Since yesterday sent the browser core introduction of the essay, Restless, want to write a JS directly to identify the user's browser version.

Writing but found a lot of places, such as the ie10-version is to follow the conventional only support attachevent and not support AddEventListener, but to the IE11, In turn, it only supports AddEventListener and does not support attachevent. Just this point can be judged that IE is a big pit, the existence of IE11 may cause the code you wrote before the confusion. Other as originally available

var ieversion = eval ("' +/* @cc_on" + "@[email protected]*/-0") * *

Sniffer script to determine whether IE, if the value is not 0 is IE browser, but to the IE11, also directly returned 0 (that is, IE11 no longer recognize @cc_on this IE unique conditional compilation statement) ....

Also, the last article mentions that opera has abandoned its Presto kernel since last year, and turned to the chrome kernel, resulting in The new version of opera no longer supports Window.opera, and with chrome features such as Window.chrome, the useragent also goes to "opera" and directly applies chromium/ Blink kernel useragent information (good thing is in the tail or keep a sentence opr/xx.0)

But pondering, the problem will always be solved. First solve the more easy to solve the Firefox, its useragent information is as follows:

Compare the UA information of other browser kernels it is unique to the word "firefox/xx.0", so we can judge:

Rfirefox =/(Firefox) \ ([\w.] +)/= rfirefox.exec (UA); if null) && (!) ( window.attachevent) && (!) ( Window.chrome)) && (!  (Window.opera))) {    //codes ...}

This also determines whether support for Window.attachevent and Window.chrome, Window.opera events, is to prevent other non-Firefox browser camouflage UA information, but I admit that this is difficult to achieve perfection.

Then Safari, although the UA message in Safari contains the word safari, but since Google's browser is the branch of the Apple browser kernel WebKit, Chrome's UA message also contains the safari Word:

This situation can only be "different", you can see the UA information of Safari in "safari/..." before the "version/...", and Chrome's UA information is not, so you can write:

Rsafari =/version\/([\w.] +). * (Safari)/= rsafari.exec (UA); if null) && (!) ( window.attachevent) && (!) ( Window.chrome)) && (!  (Window.opera))) {     //...}


Next, Chrome and opera, here's a little bit of a headache .... It was Chrome's good friend Opera, which also started using the chromium or blink engine, causing the UA information and the support for the BOM to be almost identical (this is no nonsense, the kernel is the same), but it can still be different from UA:

So we can write this (note that opera also takes care of the old version, which is the case with the Presto kernel):

Ropera =/(opera). +version\/([\w.] +)/; Rnewopera=/(OPR) \ (. +)/; Rchrome=/(chrome) \ ([\w.] +)/; Matchbs=ropera.exec (UA);if((Matchbs! =NULL) && (!(window.attachevent))) {//old opera recognitionreturn{Browser:matchbs[1] | | "", Version:matchbs[2] | | " 0 " };} Matchbs=rchrome.exec (UA);
if((Matchbs! =NULL) && (!! (Window.chrome)) && (!(window.attachevent)) {//chrome identification matchBS2=rnewopera.exec (UA);if(MatchBS2 = =NULL)//new opera recognitionreturn{Browser:matchbs[1] | | "", Version:matchbs[2] | | " 0 " };Elsereturn{browser: "Opera", version:matchbs2[2] | | "0" };}


Finally, ie identification, IE is a big pit (red box is recommended for judging the place):

By the know, IE6/7 from the MSIE version of the direct judge can, starting from IE8 more Trident information, then IE8-IE11 only need to determine the Trident version number. Then we can write our own two judgments, first determine whether ie--that the UA information contains MSIE information or Trident information (note that IE11 has removed MSIE information), and then determine whether ie7-or ie8+:

Rmsie =/(MSIE\S|TRIDENT\/7) ([\w.] +)/; Rtrident=/(Trident) \ ([\w.] +)/; Matchbs=rmsie.exec (UA);if(Matchbs! =NULL) {matchBS2=rtrident.exec (UA); if(MatchBS2! =NULL){  Switch(matchbs2[2]){   Case"4.0":return{browser: "IE", Version: "8"}; Break;  Case"5.0":return{browser: "IE", Version: "9"}; Break;  Case"6.0":return{browser: "IE", Version: "10"}; Break;  Case"7.0":return{browser: "IE", Version: "11"}; Break; default:return{browser: "IE", Version: "Undefined" }; }  }  Else  return{browser: "IE", version:matchbs[2] | | "0" };}

All the code below is available for reference:

<script type= "Text/javascript" >varUserAgent =navigator.useragent, Rmsie=/(MSIE\S|TRIDENT\/7) ([\w.] +)/, Rtrident=/(Trident) \ ([\w.] +)/, Rfirefox=/(Firefox) \ ([\w.] +)/, Ropera=/(opera). +version\/([\w.] +)/, Rnewopera=/(OPR) \ (. +)/, Rchrome=/(chrome) \ ([\w.] +)/, Rsafari=/version\/([\w.] +). * (Safari)/; varMATCHBS,MATCHBS2; varBrowser; varversion; varUA =useragent.tolowercase (); varUamatch =function(UA) {Matchbs=rmsie.exec (UA); if(Matchbs! =NULL) {matchBS2=rtrident.exec (UA); if(MatchBS2! =NULL){                            Switch(matchbs2[2]){                                 Case"4.0":return{browser: "IE", Version: "8"}; Break;  Case"5.0":return{browser: "IE", Version: "9"}; Break;  Case"6.0":return{browser: "IE", Version: "10"}; Break;  Case"7.0":return{browser: "IE", Version: "11"}; Break; default:return{browser: "IE", Version: "Undefined" }; }                        }                        Else                        return{browser: "IE", version:matchbs[2] | | "0" }; } Matchbs=rfirefox.exec (UA); if((Matchbs! =NULL) && (! ( window.attachevent) && (!) ( Window.chrome)) && (!(Window.opera))) {return{Browser:matchbs[1] | | "", Version:matchbs[2] | | " 0 " }; } Matchbs=ropera.exec (UA); if((Matchbs! =NULL) && (!(window.attachevent))) {return{Browser:matchbs[1] | | "", Version:matchbs[2] | | " 0 " }; } Matchbs=rchrome.exec (UA); if((Matchbs! =NULL) && (!! (Window.chrome)) && (!(window.attachevent))) {matchBS2=rnewopera.exec (UA); if(MatchBS2 = =NULL)                        return{Browser:matchbs[1] | | "", Version:matchbs[2] | | " 0 " }; Else                        return{browser: "Opera", version:matchbs2[2] | | "0" }; } Matchbs=rsafari.exec (UA); if((Matchbs! =NULL) && (! ( window.attachevent) && (!) ( Window.chrome)) && (!(Window.opera))) {return{Browser:matchbs[2] | | "", Version:matchbs[1] | | " 0 " }; }                    if(Matchbs! =NULL) {                        return{browser: "undefined", Version: "Browser" }; }                }                varBrowsermatch =Uamatch (Useragent.tolowercase ()); if(browsermatch.browser) {browser=Browsermatch.browser; Version=browsermatch.version; } document.write (Browser+version); </script>
View Code


However, it must be said that the identification of various versions of the browser is a very troublesome thing, the above code can help you cope with most of the situation, but if you encounter some special situations (such as the browser disguised UA information), the browser can not identify the specific version.

Welcome to communicate and explore ~

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.