我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
当前回答
我写这个是为了满足我的需要。
它可以获取信息,比如是否是移动设备或是否有视网膜显示器
试一试
var nav = {
isMobile:function(){
return (navigator.userAgent.match(/iPhone|iPad|iPod|Android|BlackBerry|Opera Mini|IEMobile/i) != null);
},
isDesktop:function(){
return (navigator.userAgent.match(/iPhone|iPad|iPod|Android|BlackBerry|Opera Mini|IEMobile/i) == null);
},
isAndroid: function() {
return navigator.userAgent.match(/Android/i);
},
isBlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
isIOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
isOpera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
isWindows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
isRetina:function(){
return window.devicePixelRatio && window.devicePixelRatio > 1;
},
isIPad:function(){
isIPad = (/ipad/gi).test(navigator.platform);
return isIPad;
},
isLandscape:function(){
if(window.innerHeight < window.innerWidth){
return true;
}
return false;
},
getIOSVersion:function(){
if(this.isIOS()){
var OSVersion = navigator.appVersion.match(/OS (\d+_\d+)/i);
OSVersion = OSVersion[1] ? +OSVersion[1].replace('_', '.') : 0;
return OSVersion;
}
else
return false;
},
isStandAlone:function(){
if(_.is(navigator.standalone))
return navigator.standalone;
return false;
},
isChrome:function(){
var isChrome = (/Chrome/gi).test(navigator.appVersion);
var isSafari = (/Safari/gi).test(navigator.appVersion)
return isChrome && isSafari;
},
isSafari:function(){
var isSafari = (/Safari/gi).test(navigator.appVersion)
var isChrome = (/Chrome/gi).test(navigator.appVersion)
return !isChrome && isSafari;
}
}
其他回答
这里有更好的兼容性@kennebec snippet; 将返回浏览器名称和版本(返回72而不是72.0.3626.96)。
在Safari, Chrome, Opera, Firefox, IE, Edge, UCBrowser上测试,也在移动设备上测试。
function browser() {
var userAgent = navigator.userAgent,
match = userAgent.match(/(opera|chrome|crios|safari|ucbrowser|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
result = {},
tem;
if (/trident/i.test(match[1])) {
tem = /\brv[ :]+(\d+)/g.exec(userAgent) || [];
result.name = "Internet Explorer";
} else if (match[1] === "Chrome") {
tem = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
if (tem && tem[1]) {
result.name = tem[0].indexOf("Edge") === 0 ? "Edge" : "Opera";
}
}
if (!result.name) {
tem = userAgent.match(/version\/(\d+)/i); // iOS support
result.name = match[0].replace(/\/.*/, "");
if (result.name.indexOf("MSIE") === 0) {
result.name = "Internet Explorer";
}
if (userAgent.match("CriOS")) {
result.name = "Chrome";
}
}
if (tem && tem.length) {
match[match.length - 1] = tem[tem.length - 1];
}
result.version = Number(match[match.length - 1]);
return result;
}
这是对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转换上自动对其进行测试。
我们可以一起做这件事。
<script type="text/javascript">
var version = navigator.appVersion;
alert(version);
</script>
我想分享我为我必须解决的问题而编写的代码。它在大多数主流浏览器中都进行了测试,对我来说,它就像一个魅力!
似乎这段代码与其他答案非常相似,但它进行了修改,以便我可以使用它而不是我最近错过的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
在纯Javascript中,您可以在导航器上执行RegExp匹配。使用userAgent查找Firefox版本:
var uMatch = navigator.userAgent.match(/Firefox\/(.*)$/),
ffVersion;
if (uMatch && uMatch.length > 1) {
ffVersion = uMatch[1];
}
如果不是Firefox浏览器,ffVersion将未定义。
参见工作示例→