我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。

如何检测这样的浏览器版本?


当前回答

您可以看到浏览器显示的内容,并使用该信息记录或测试多个浏览器。

navigator.sayswho= (function(){ var ua= navigator.userAgent; var tem; var 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 'IE '+(tem[1] || ''); } if(M[1]=== 'Chrome'){ tem= ua.match(/\b(OPR|Edge)\/(\d+)/); if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera'); } 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 M.join(' '); })(); console.log(navigator.sayswho); // outputs: `Chrome 62`

其他回答

更新的代码,以检测浏览器从iOS。

navigator.sayswho= (function(){
var ua= navigator.userAgent;
var tem; 
var 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 'IE '+(tem[1] || '');
}
if(M[1]=== 'Chrome'){
    tem= ua.match(/\b(OPR)\/(\d+)/);
    if(tem!= null) return 'Opera '+ tem.slice(1)[1];
    tem= ua.match(/\b(Edg[a-zA-Z]{0,3})\/(\d+)/);
    if(tem!= null) return 'Edge '+ tem.slice(1)[1];
} else if(M[1]=== 'Safari') {
    tem= ua.match(/\b(EdgiOS)\/(\d+)/);
    if(tem!= null) return 'Edge '+ tem.slice(1)[1];
    tem= ua.match(/\b(FxiOS)\/(\d+)/);
    if(tem!= null) return 'Firefox '+ tem.slice(1)[1];
    tem= ua.match(/\b(CriOS)\/(\d+)/);
    if(tem!= null) return 'Chrome '+ tem.slice(1)[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 M.join(' ');

}) ();

这是对Kennebec答案的改进。

mbrowser=function(){
    this.spec_string=   navigator.userAgent;
    this.name=          this.get_name();
    this.version=       this.get_version();
    };

mbrowser.prototype.get_name=function(){
    var spec_string=this.spec_string;

    var matches=spec_string.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    
    // Work with the matches.
    matches=matches[2]? [matches[1], matches[2]]: [navigator.appName, navigator.appVersion, '-?'];
        
    // Trident.
    if(/trident/i.test(matches[1])){
        var temp=/\brv[ :]+(\d+)/g.exec(spec_string) || [];
        return 'IE';
        }
    
    // Chrome.
    if(matches[1]==='Chrome'){
        var temp=spec_string.match(/\bOPR|Edge\/(\d+)/)
        if(temp!=null)   {return 'Opera';}
        }   

    if((temp=spec_string.match(/version\/(\d+)/i))!=null){
        matches.splice(1,1,temp[1]);
        }
                                                                                                                       
    var name=matches[0];

    return name;
    };


mbrowser.prototype.get_version=function(){
    var spec_string=this.spec_string;

    var matches=spec_string.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; 

    // Work with the matches.
    matches=matches[2]? [matches[1], matches[2]]: [navigator.appName, navigator.appVersion, '-?'];

    // Trident.
    if(/trident/i.test(matches[1])){
        var temp=/\brv[ :]+(\d+)/g.exec(spec_string) || []; 
        var version=(temp[1]||'');
        return version;
        }   

    // Chrome.
    if(matches[1]==='Chrome'){
        var temp=spec_string.match(/\bOPR|Edge\/(\d+)/)
        var version=temp[1];
        if(temp!=null)   {return version;}
        }   

    if((temp=spec_string.match(/version\/(\d+)/i))!=null){
        matches.splice(1,1,temp[1]);                                                                                   
        }

    var version=matches[1];

    return version;
    };

// m=module.
var browser=new mbrowser();
console.log(browser.name);    // Chrome
console.log(browser.version); // 109

这段代码从spec_string (navigator.userAgent)中推导出浏览器名称和编号。但是有各种各样的spec_string,它们有各自的浏览器名称和编号。我没有条件去检查,但是其中的一小部分。如果你能发布一个你知道的浏览器名称和编号的spec_string,那就太好了。然后我可以相应地更新代码。

然后,我将按照以下方式慢慢地构建spec_string项的列表。

'spec_string1'=>[name,number],
'spec_string2'=>[name,number],

以后,无论何时对代码进行更改,都可以在所有已知的spec_string转换上自动对其进行测试。

我们可以一起做这件事。

对于任何使用angular的PWA应用程序,你都可以在index.html -的body部分中检查浏览器是否受支持

<body>
    <div id="browser"></div>
    <script>
        var operabrowser = true;
        operabrowser = (navigator.userAgent.indexOf('Opera Mini') > -1);
        if (operabrowser) {
            txt = "<p>Browser not supported use different browser...</p>";
            document.getElementById("browser").innerHTML = txt;
        }
    </script>
</body>

使用这个:http://www.quirksmode.org/js/detect.html

alert(BrowserDetect.browser); // will say "Firefox"
alert(BrowserDetect.version); // will say "3" or "4"

我想分享我为我必须解决的问题而编写的代码。它在大多数主流浏览器中都进行了测试,对我来说,它就像一个魅力!

似乎这段代码与其他答案非常相似,但它进行了修改,以便我可以使用它而不是我最近错过的jquery浏览器对象,当然它是上述代码的组合,从我的部分我做了一些改进:

(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