我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
当前回答
使用这个: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
这是对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转换上自动对其进行测试。
我们可以一起做这件事。
var ua = navigator.userAgent;
if (/Firefox\//.test(ua))
var Firefox = /Firefox\/([0-9\.A-z]+)/.exec(ua)[1];
浏览器JavaScript库提供了此功能。
if (bowser.msie && bowser.version <= 6) {
alert('Hello China');
}
它似乎保养得很好。
这结合了kennebec的(K)答案和Hermann Ingjaldsson的(H)答案:
维护原始答案的最小代码。(K) 使用Microsoft Edge (K) 扩展导航器对象,而不是创建新的变量/对象。(K) 将浏览器版本和名称分离为独立的子对象。(H)
navigator.browserSpecs = (function(){ 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(/\b(OPR|Edge)\/(\d+)/); if(tem != null) return {name:tem[1].replace('OPR', 'Opera'),version:tem[2]}; } 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]}; })(); console.log(navigator.browserSpecs); //Object { name: "Firefox", version: "42" } if (navigator.browserSpecs.name == 'Firefox') { // Do something for Firefox. if (navigator.browserSpecs.version > 42) { // Do something for Firefox versions greater than 42. } } else { // Do something for all other browsers. }