How can you detect the version of a browser?

后端 未结 28 2103
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 23:35

I\'ve been searching around for code that would let me detect if the user visiting the website has Firefox 3 or 4. All I have found is code to detect the type of browser but

相关标签:
28条回答
  • 2020-11-22 00:26

    I was looking for a solution for myself, since jQuery 1.9.1 and above have removed the $.browser functionality. I came up with this little function that works for me. It does need a global variable (I've called mine _browser) in order to check which browser it is. I've written a jsfiddle to illustrate how it can be used, of course it can be expanded for other browsers by just adding a test for _browser.foo, where foo is the name of the browser. I did just the popular ones.

    detectBrowser()

    _browser = {};
    
    function detectBrowser() {
      var uagent = navigator.userAgent.toLowerCase(),
          match = '';
    
      _browser.chrome  = /webkit/.test(uagent)  && /chrome/.test(uagent)      &&
                         !/edge/.test(uagent);
    
      _browser.firefox = /mozilla/.test(uagent) && /firefox/.test(uagent);
    
      _browser.msie    = /msie/.test(uagent)    || /trident/.test(uagent)     ||
                         /edge/.test(uagent);
    
      _browser.safari  = /safari/.test(uagent)  && /applewebkit/.test(uagent) &&
                         !/chrome/.test(uagent);
    
      _browser.opr     = /mozilla/.test(uagent) && /applewebkit/.test(uagent) &&
                         /chrome/.test(uagent)  && /safari/.test(uagent)      &&
                         /opr/.test(uagent);
    
      _browser.version = '';
    
      for (x in _browser) {
        if (_browser[x]) {
    
          match = uagent.match(
                    new RegExp("(" + (x === "msie" ? "msie|edge" : x) + ")( |\/)([0-9]+)")
                  );
    
          if (match) {
            _browser.version = match[3];
          } else {
            match = uagent.match(new RegExp("rv:([0-9]+)"));
            _browser.version = match ? match[1] : "";
          }
          break;
        }
      }
      _browser.opera = _browser.opr;
      delete _browser.opr;
    }
    

    To check if the current browser is Opera you would do

    if (_browser.opera) { // Opera specific code }
    

    Edit Fixed the formatting, fixed the detection for IE11 and Opera/Chrome, changed to browserResult from result. Now the order of the _browser keys doesn't matter. Updated jsFiddle link.

    2015/08/11 Edit Added new testcase for Internet Explorer 12 (EDGE), fixed a small regexp problem. Updated jsFiddle link.

    0 讨论(0)
  • 2020-11-22 00:26
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    var browserName  = navigator.appName;
    var fullVersion  = ''+parseFloat(navigator.appVersion);
    var majorVersion = parseInt(navigator.appVersion,10);
    var nameOffset,verOffset,ix;
    
    // In Opera, the true version is after "Opera" or after "Version"
    
    if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
     browserName = "Opera";
     fullVersion = nAgt.substring(verOffset+6);
     if ((verOffset=nAgt.indexOf("Version"))!=-1)
       fullVersion = nAgt.substring(verOffset+8);
    }
    // In MSIE, the true version is after "MSIE" in userAgent
    
    else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
     browserName = "Microsoft Internet Explorer";
     fullVersion = nAgt.substring(verOffset+5);
    }
    // In Chrome, the true version is after "Chrome"
    
    else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
     browserName = "Chrome";
     fullVersion = nAgt.substring(verOffset+7);
    }
    // In Safari, the true version is after "Safari" or after "Version"
    
    else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
     browserName = "Safari";
     fullVersion = nAgt.substring(verOffset+7);
     if ((verOffset=nAgt.indexOf("Version"))!=-1)
       fullVersion = nAgt.substring(verOffset+8);
    }
    // In Firefox, the true version is after "Firefox"
    
    else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
     browserName = "Firefox";
     fullVersion = nAgt.substring(verOffset+8);
    }
    // In most other browsers, "name/version" is at the end of userAgent
    
    else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <
              (verOffset=nAgt.lastIndexOf('/')) )
    {
     browserName = nAgt.substring(nameOffset,verOffset);
     fullVersion = nAgt.substring(verOffset+1);
     if (browserName.toLowerCase()==browserName.toUpperCase()) {
      browserName = navigator.appName;
     }
    }
    
    // trim the fullVersion string at semicolon/space if present
    
    if ((ix=fullVersion.indexOf(";"))!=-1)
       fullVersion=fullVersion.substring(0,ix);
    if ((ix=fullVersion.indexOf(" "))!=-1)
       fullVersion=fullVersion.substring(0,ix);
    
    majorVersion = parseInt(''+fullVersion,10);
    if (isNaN(majorVersion)) {
     fullVersion  = ''+parseFloat(navigator.appVersion);
     majorVersion = parseInt(navigator.appVersion,10);
    }
    
    document.write(''
     +'Browser name  = '+browserName+'<br>'
     +'Full version  = '+fullVersion+'<br>'
     +'Major version = '+majorVersion+'<br>'
     +'navigator.appName = '+navigator.appName+'<br>'
     +'navigator.userAgent = '+navigator.userAgent+'<br>'
    )
    

    See the demo here..http://jsfiddle.net/hw4jM/3/

    0 讨论(0)
  • 2020-11-22 00:27
    function BrowserCheck()
    {
        var N= navigator.appName, ua= navigator.userAgent, tem;
        var M= ua.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
        if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) {M[2]=tem[1];}
        M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
        return M;
    }
    

    This will return an array, first element is the browser name, second element is the complete version number in string format.

    0 讨论(0)
  • 2020-11-22 00:27

    In pure Javascript you can do a RegExp match on the navigator.userAgent to find the Firefox version:

    var uMatch = navigator.userAgent.match(/Firefox\/(.*)$/),
        ffVersion;
    if (uMatch && uMatch.length > 1) {
        ffVersion = uMatch[1];
    }
    

    ffVersion will be undefined if not a Firefox browser.

    See working example →

    0 讨论(0)
提交回复
热议问题