How can you detect the version of a browser?

后端 未结 28 2051
佛祖请我去吃肉
佛祖请我去吃肉 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:00

    Look at navigator.userAgent - Firefox/xxx.xxx.xxx is specified right at the end.

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

    I want to share this code I wrote for the issue I had to resolve. It was tested in most of the major browsers and works like a charm, for me!

    It may seems that this code is very similar to the other answers but it modifyed so that I can use it insted of the browser object in jquery which missed for me recently, of course it is a combination from the above codes, with little improvements from my part I made:

    (function($, ua){
    
    var M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
        tem, 
        res;
    
    if(/trident/i.test(M[1])){
        tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
        res = 'IE ' + (tem[1] || '');
    }
    else if(M[1] === 'Chrome'){
        tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
        if(tem != null) 
            res = tem.slice(1).join(' ').replace('OPR', 'Opera');
        else
            res = [M[1], M[2]];
    }
    else {
        M = M[2]? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
        if((tem = ua.match(/version\/(\d+)/i)) != null) M = M.splice(1, 1, tem[1]);
        res = M;
    }
    
    res = typeof res === 'string'? res.split(' ') : res;
    
    $.browser = {
        name: res[0],
        version: res[1],
        msie: /msie|ie/i.test(res[0]),
        firefox: /firefox/i.test(res[0]),
        opera: /opera/i.test(res[0]),
        chrome: /chrome/i.test(res[0]),
        edge: /edge/i.test(res[0])
    }
    
    })(typeof jQuery != 'undefined'? jQuery : window.$, navigator.userAgent);
    
     console.log($.browser.name, $.browser.version, $.browser.msie); 
    // if IE 11 output is: IE 11 true
    
    0 讨论(0)
  • 2020-11-22 00:02

    This is an improvement on Kennebec's answer.

    function get_browser() {
        var ua=navigator.userAgent,tem,M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; 
        if(/trident/i.test(M[1])){
            tem=/\brv[ :]+(\d+)/g.exec(ua) || []; 
            return {name:'IE',version:(tem[1]||'')};
            }   
        if(M[1]==='Chrome'){
            tem=ua.match(/\bOPR|Edge\/(\d+)/)
            if(tem!=null)   {return {name:'Opera', version:tem[1]};}
            }   
        M=M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
        if((tem=ua.match(/version\/(\d+)/i))!=null) {M.splice(1,1,tem[1]);}
        return {
          name: M[0],
          version: M[1]
        };
     }
    
    var browser=get_browser(); // browser.name = 'Chrome'
                               // browser.version = '40'
    
    console.log(browser);

    This way you can shield yourself from the obscurity of the code.

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

    Use this: http://www.quirksmode.org/js/detect.html

    alert(BrowserDetect.browser); // will say "Firefox"
    alert(BrowserDetect.version); // will say "3" or "4"
    
    0 讨论(0)
  • 2020-11-22 00:03
    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 15+, the true version is after "OPR/" 
    if ((verOffset = nAgt.indexOf("OPR/")) != -1) {
        browserName = "Opera";
        fullVersion = nAgt.substring(verOffset + 4);
    }
    // In older Opera, the true version is after "Opera" or after "Version"
    else 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 = "Google 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 = "Mozilla 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);
    }
    
    0 讨论(0)
  • 2020-11-22 00:04
    <script type="text/javascript">
    var version = navigator.appVersion;
    alert(version);
    </script>
    
    0 讨论(0)
提交回复
热议问题