JavaScript implementation get Browser version, OS type _javascript tips

Source: Internet
Author: User

The code is very concise, functional very practical, here is not much nonsense, directly to the code:

/** * Created by the Administrator on 15-1-12. */function Broswerutil () {} Broswerutil = {//Detect browser version getbrowserversion:function () {var agent = Navigator.use
    Ragent.tolowercase ();
    var arr = [];
    var Browser = "";
    var bversion = "";
    var verinnum = ""; IE if (Agent.indexof ("MSIE") > 0) {var regstr_ie =/msie [\d.]
      +;/gi;
      Browser = "IE"; Bversion = "" + Agent.match (regstr_ie)}//firefox else if (Agent.indexof ("Firefox") > 0) {var regst R_FF =/firefox\/[\d.]
      +/gi;
      Browser = "Firefox";
    Bversion = "" + Agent.match (REGSTR_FF); }//chrome else if (Agent.indexof ("Chrome") > 0) {var regstr_chrome =/chrome\/[\d.]
      +/gi;
      Browser = "Chrome";
    Bversion = "" + Agent.match (regstr_chrome); //safari Else if (Agent.indexof ("Safari") > 0 && agent.indexof ("Chrome") < 0) {var regstr_ SAF =/version\/[\d.]
      +/gi;
      Browser = "Safari"; Bversion ="" + Agent.match (REGSTR_SAF); }//opera else if (Agent.indexof ("Opera") >= 0) {var Regstr_opera =/version\/[\d.]
      +/gi;
      Browser = "Opera";
    Bversion = "" + Agent.match (Regstr_opera);
      else {var browser = navigator.appname;
        if (browser = "Netscape") {var version = Agent.split (";");
        var trim_version = version[7].replace (/[]/g, ""); var rvstr = Trim_version.match (/[\d\.]
        /g). ToString ();
        var rv = rvstr.replace (/[,]/g, "");
        bversion = RV; Browser = "IE"}} verinnum = (bversion + ""). Replace (/[^0-9.]
    /ig, "");
    Arr.push (Browser);
    Arr.push (Verinnum);
  return arr;
    ///Detect if XX browser WB: (function () {var useragent = navigator.userAgent.toLowerCase (); return {isIE6:/msie 6.0/.test (useragent),//IE6 isIE7:/msie 7.0/.test (useragent),//IE7 isIE8:/msi E 8.0/.test (useragent),//IE8 isIE9:/msie 9.0/.test (useragent),//IE9 Isie/msie 10.0/.test (useragent),//IE10 isIE11:/msie 11.0/.test (useragent),//IE11 ISLB:/lbbrowser/.test (U seragent),//Cheetah browser isuc:/ucweb/.test (useragent),//UC Browser is360:/360se/.test (useragent),//360 browser is Baidu:/bidubrowser/.test (useragent),//Baidu browser Issougou:/metasr/.test (useragent),//Sogou browser ischrome:/chrome/ . Test (Useragent.substr ( -33, 6)),//Chrome browser Isfirefox:/firefox/.test (useragent),//Firefox browser Isopera:/opera/ 
      . Test (useragent),//Opera browser issafire:/safari/.test (useragent) &&!/chrome/.test (useragent),//Safire browser
  ISQQ:/qqbrowser/.test (useragent)//qq Browser}; } (),//detects the current operating system Currentsystem: (function () {var system = {Win:false, mac:false, Xll:false 
      , Iphone:false, Ipoad:false, Ipad:false, Ios:false, Android:false, Nokian:false,
    Winmobile:false, Wii:false, ps:false}; var ua = navigator.useragent;
    Detection platform var p = navigator.platform;
    System.win = P.indexof (' win ') = = 0;
    System.mac = P.indexof (' mac ') = = 0;
    System.xll = (p.indexof (' xll ') = = 0 | | p.indexof (' Linux ') = = 0); Detects the Windows operating system if (System.win) {if (/win) {if (?:d ows)? ( [^do] {2}) \s?
              (\d+\.\d+)/.test (UA)) {if (regexp[' $] = = ' NT ') {switch (regexp[' $ ')] {case ' 5.0 ':
              System.win = ' 2000 ';
            Break
              Case ' 5.1 ': System.win = ' XP ';
            Break
              Case ' 6.0 ': System.win = ' Vista ';
            Break
              Case ' 6.1 ': System.win = ' 7 ';
            Break
              Case ' 6.2 ': System.win = ' 8 ';
            Break
              Default:system.win = ' NT ';
          Break
        Or else if (regexp[' $ '] = = ' 9x ') {System.win = ' ME ';
        else {System.win = regexp[' $ '];
   }   }//Mobile device System.iphone = ua.indexof (' iphone ') >-1;
    System.ipod = ua.indexof (' ipod ') >-1;
    System.ipad = Ua.indexof (' ipad ') >-1;
    System.nokian = Ua.indexof (' Nokian ') >-1;
    Windows Mobile if (System.win = = ' CE ') {system.winmobile = System.win;
        else if (System.win = = ' Ph ') {if (/windows phone OS (\d+.\d)/i.test (UA)) {System.win = ' phone ';
      System.winmobile = parsefloat (regexp[' $ '); }//Detect iOS version if (System.mac && ua.indexof (' Mobile ') >-1) {if/CPU (?: IPhone)?
      OS (\d+_\d+)/i.test (UA) {System.ios = parsefloat (regexp[' ' '].replace ' (' _ ', '. '));    else {System.ios = 2; Can not really detect, so can only guess}//Detect Android Version if (/android (\d+\.\d+)/i.test (UA)) {system.android = Pars
    Efloat (regexp[' $ ');
    }//game system system.wii = Ua.indexof (' wii ') >-1;
    system.ps =/playstation/i.test (UA); return {System: System}}) ()}
 

The above is the JavaScript packaged version of the browser and the operating system type of code library, I hope you can enjoy.

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.