Use JS to identify each version of the browser

Source: Internet
Author: User
Tags contains return version window

For example, the ie10-version is in accordance with the general only support attachevent and not support AddEventListener, but to IE11, but in turn only support AddEventListener and not support attachevent. This alone can tell IE is a big hole, the existence of IE11 may cause the code you wrote before has been confused. Other as originally available

var ieversion = eval ("" "+/* @cc_on" + "@_jscript_version@*/-0") *1

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

There is also the previous article that opera has abandoned its own Presto kernel last year, and instead followed the use of the chrome kernel, the result is that the new version of opera no longer supports Window.opera, and follow the Chrome browser support Window.chrome and other series of chrome features, even useragent words also went to "opera" and directly apply chromium/ Blink kernel useragent information (good thing is in the tail or keep a sentence opr/xx.0)

However, the problem will always be solved. First to solve the easier to solve the Firefox, its useragent information is as follows:

Compared to other browser kernel UA information It is unique "firefox/xx.0" words, so we can judge:

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

It also determines whether the window.attachevent and Window.chrome, Window.opera events are supported to prevent other non-Firefox browsers from disguising UA information, but I admit it's hard to be perfect.

Then there is safari, although Safari's UA message contains the word safari, but because Google's browser is a branch of Apple's browser kernel WebKit, Chrome's UA message also contains safari words:

This situation can only "look different", you will see Safari UA information in "safari/..." before a "version/...", and chrome UA information is not, so you can write:

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

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

So we can write this (note that opera also takes into account the old version, that is, using the Presto kernel):

Ropera =/(opera). +version\/([\w.] 
+)/;
Rnewopera =/(OPR) \/(. +)/; Rchrome =/(Chrome) \/([\w.]
+)/;
Matchbs = Ropera.exec (UA); if ((Matchbs!= null) && (!) ( window.attachevent)) {              //
Old Opera recognizes return {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 recognition    return {
Browser:matchbs[1] "", Version:matchbs[2] "0"};    Else    return {broWser: "Opera", Version:matchbs2[2] "0"}; }

Finally, IE's identification bar, IE is a big pit (red box part is recommended for the judge of the place):

As can be seen from the above figure, IE6/7 from the MSIE version of the direct judge, from the IE8 began with more than a Trident information, then IE8-IE11 just Judge Trident version number. Then we can write two judgements on our own, first to determine whether 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\STRIDENT\/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: "Ten"};break;
Case "7.0": Return {browser: "IE", Version: "One"};break;
Default:return {browser: "IE", Version: "Undefined"};
else return
{browser: "IE", Version:matchbs[2]  "0"}

All the code below is attached for reference:

<script type= "Text/javascript" >                  var useragent = navigator.useragent,                  Rmsie =/(MSIE\STRIDENT\/7) ([\w.] +)/,                 rtrident =/( Trident) \/([\w.] +)/,                 Rfirefox =/( Firefox) \/([\w.] +)/,                 Ropera =/(opera). +version\/([\w.] +)/,                 Rnewopera =/(OPR) \/(. +)/,                 rchrome =/( Chrome) \/([\w.] +)/,                 Rsafari =/version\/([\w.]
+). * (Safari)/;
                var matchbs,matchbs2;
                var browser;
                var version;                 var ua =
Useragent.tolowercase ();                 var uamatch = 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: "Ten"};break;                                  case "7.0": Return {browser: "IE",
Version: "One"};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"};                     }                }                  var browsermatch = Uamatch (
Useragent.tolowercase ());                 if ( Browsermatch.browser) {              
      browser = Browsermatch.browser;                   
  Version = Browsermatch.version;                }                  document.write (browser+version);         </script>

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 to deal with most of the situation, but if you encounter certain special scenarios (such as browser camouflage UA information), you can not identify the specific version of the browser.



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.