我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
当前回答
这结合了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. }
其他回答
navigator.sayswho= (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 '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`
这让我有了火狐的版本,
let versionNumber = parseInt(window.navigator.userAgent.match(/Firefox\/([0-
9]+)\./)[1]);
jQuery可以很好地处理这个问题(jQuery.browser)
var ua = $.browser;
if ( ua.mozilla && ua.version.slice(0,3) == "1.9" ) {
alert( "Do stuff for firefox 3" );
}
编辑:正如Joshua在他的评论下面所写的,jQuery。jQuery自1.9版起不再支持browser属性(详情请参阅jQuery 1.9发布说明)。 jQuery开发团队建议使用更完整的方法,例如使用Modernizr库调整UI。
我用ASP代码做了一个检测浏览器,浏览器版本,OS和OS版本的脚本。 我在ASP中这样做的原因是因为我想将数据存储在日志数据库中。 所以我必须检测浏览器服务器端。
代码如下:
on error resume next
ua = lcase(Request.ServerVariables("HTTP_USER_AGENT"))
moz = instr(ua,"mozilla")
ffx = instr(ua,"firefox")
saf = instr(ua,"safari")
crm = instr(ua,"chrome")
max = instr(ua,"maxthon")
opr = instr(ua,"opera")
ie4 = instr(ua,"msie 4")
ie5 = instr(ua,"msie 5")
ie6 = instr(ua,"msie 6")
ie7 = instr(ua,"msie 7")
ie8 = instr(ua,"trident/4.0")
ie9 = instr(ua,"trident/5.0")
if moz>0 then
BrowserType = "Mozilla"
BrVer = mid(ua,moz+8,(instr(moz,ua," ")-(moz+8)))
end if
if ffx>0 then
BrowserType = "FireFox"
BrVer = mid(ua,ffx+8)
end if
if saf>0 then
BrowserType = "Safari"
BrVerPlass = instr(ua,"version")
BrVer = mid(ua,BrVerPlass+8,(instr(BrVerPlass,ua," ")-(BrVerPlass+8)))
end if
if crm>0 then
BrowserType = "Chrome"
BrVer = mid(ua,crm+7,(instr(crm,ua," ")-(crm+7)))
end if
if max>0 then
BrowserType = "Maxthon"
BrVer = mid(ua,max+8,(instr(max,ua," ")-(max+8)))
end if
if opr>0 then
BrowserType = "Opera"
BrVerPlass = instr(ua,"presto")
BrVer = mid(ua,BrVerPlass+7,(instr(BrVerPlass,ua," ")-(BrVerPlass+7)))
end if
if ie4>0 then
BrowserType = "Internet Explorer"
BrVer = "4"
end if
if ie5>0 then
BrowserType = "Internet Explorer"
BrVer = "5"
end if
if ie6>0 then
BrowserType = "Internet Explorer"
BrVer = "6"
end if
if ie7>0 then
BrowserType = "Internet Explorer"
BrVer = "7"
end if
if ie8>0 then
BrowserType = "Internet Explorer"
BrVer = "8"
if ie7>0 then BrVer = BrVer & " (in IE7 compability mode)"
end if
if ie9>0 then
BrowserType = "Internet Explorer"
BrVer = "9"
if ie7>0 then BrVer = BrVer & " (in IE7 compability mode)"
if ie8>0 then BrVer = BrVer & " (in IE8 compability mode)"
end if
OSSel = mid(ua,instr(ua,"(")+1,(instr(ua,";")-instr(ua,"("))-1)
OSver = mid(ua,instr(ua,";")+1,(instr(ua,")")-instr(ua,";"))-1)
if BrowserType = "Internet Explorer" then
OSStart = instr(ua,";")
OSStart = instr(OSStart+1,ua,";")
OSStopp = instr(OSStart+1,ua,";")
OSsel = mid(ua,OSStart+2,(OSStopp-OSStart)-2)
end if
Select case OSsel
case "windows nt 6.1"
OS = "Windows"
OSver = "7"
case "windows nt 6.0"
OS = "Windows"
OSver = "Vista"
case "windows nt 5.2"
OS = "Windows"
OSver = "Srv 2003 / XP x64"
case "windows nt 5.1"
OS = "Windows"
OSver = "XP"
case else
OS = OSSel
End select
Response.write "<br>" & ua & "<br>" & BrowserType & "<br>" & BrVer & "<br>" & OS & "<br>" & OSver & "<br>"
'Use the variables here for whatever you need........
我写这个是为了满足我的需要。
它可以获取信息,比如是否是移动设备或是否有视网膜显示器
试一试
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;
}
}