Browser detection JS code (compatible with the current major mainstream browsers) _javascript skills

Source: Internet
Author: User

The example of this article describes the JS code to achieve browser detection, sharing for everyone to reference, the specific content as follows

var browsermatch = {init:function () {this.browser = This.getbrowser (). browser | |
    "An Unknown Browser"; This.version = This.getbrowser (). version | |
    "An Unknown Version"; This. OS = This.getos () | |
  "An Unknown OS";
    }, Getos:function () {if (Navigator.platform.indexOf ("Win")!=-1) return "Windows";
    if (Navigator.platform.indexOf ("Mac")!=-1) return "Mac";
    if (Navigator.platform.indexOf ("Linux")!=-1) return "Linux";
  if (Navigator.userAgent.indexOf ("IPhone")!=-1) return "Iphone/ipod"; }, Getbrowser:function () {var Rmsie =/(MSIE\S|TRIDENT\/7) ([\w\.]
    +)/; var rtrident =/(trident) \/([\w.]
    +)/; var rfirefox =/(Firefox) \/([\w.] 
    +)/; var Ropera =/(opera). +version\/([\w.]
    +)/;
    var Rnewopera =/(OPR) \/(. +)/; var rchrome =/(Chrome) \/([\w.]
    +)/; var Rsafari =/version\/([\w.]
    +). * (Safari)/;
    var ua = Navigator.userAgent.toLowerCase ();
    var matchbs,matchbs2;
    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 "};  
}           
  }
};
 Browsermatch.init ();

Call method: (first have to introduce JS file, we should all understand)

Get Browser name: Browsermatch.browser;

Get Browser version: browsermatch.version;

get the operating system: Browsermatch.os;

The above is the entire content of this article, I hope to help you learn.

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.