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

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


当前回答

基于已接受的答案,这是检测“Microsoft Edge”的更新

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|Edg)\/(\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);

其他回答

我正在为自己寻找一个解决方案,因为jQuery 1.9.1及以上已经删除了$。浏览器的功能。我想出了这个小函数。 它确实需要一个全局变量(我称之为mine _browser)来检查它是哪个浏览器。我已经写了一个jsfiddle来说明如何使用它,当然,它可以通过为_browser添加一个测试来扩展到其他浏览器。Foo,其中Foo是浏览器的名称。我只做了流行的。

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; } detectBrowser(); console.log(_browser)

检查当前浏览器是否是Opera

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

编辑修复了格式,修复了对IE11和Opera/Chrome的检测,从结果更改为browserResult。现在_browser键的顺序不重要了。更新了jsFiddle链接。

2015/08/11编辑为Internet Explorer 12 (EDGE)添加了新的测试用例,修复了一个小的regexp问题。更新了jsFiddle链接。

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

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`

这个页面似乎有一个非常漂亮的代码片段,它只使用appString和appVersion属性作为最后的手段,因为它声称它们在某些浏览器中是不可靠的。 页面上的代码如下:

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 = "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>'
)

基于已接受的答案,这是检测“Microsoft Edge”的更新

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|Edg)\/(\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);

这结合了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. }