我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
当前回答
我用它来获得实际浏览器版本的名称和编号(int):
function getInfoBrowser() { 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: 'Explorer', version: parseInt((tem[1] || '')) }; } if (M[1] === 'Chrome') { tem = ua.match(/\b(OPR|Edge)\/(\d+)/); if (tem != null) { let app = tem.slice(1).toString().split(','); return { name: app[0].replace('OPR', 'Opera'), version: parseInt(app[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 { name: M[0], version: parseInt(M[1]) }; } function getBrowser(){ let info = getInfoBrowser(); $("#i-name").html(info.name); $("#i-version").html(info.version); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" onclick="getBrowser();" value="Get Info Browser"/> <hr/> Name: <span id="i-name"></span><br/> Version: <span id="i-version"></span>
这个磨合
铬;火狐浏览器;狩猎;Internet Explorer (>= 9);歌剧;边缘
给我。
其他回答
这是Fzs2和kennebec的更新,用于新边缘铬
function get_browser() { 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(/\bEdg\/(\d+)/) if(tem!=null) {return {name:'Edge(Chromium)', version:tem[1]};} tem=ua.match(/\bOPR\/(\d+)/) if(tem!=null) {return {name:'Opera', version:tem[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 { name: M[0], version: M[1] }; } var browser=get_browser(); // browser.name = 'Edge(Chromium)' // browser.version = '86' console.log(browser);
看看navigator。userAgent - Firefox/xxx.xxx. xxx。XXX是在结尾指定的。
更新的代码,以检测浏览器从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(' ');
}) ();
在纯Javascript中,您可以在导航器上执行RegExp匹配。使用userAgent查找Firefox版本:
var uMatch = navigator.userAgent.match(/Firefox\/(.*)$/),
ffVersion;
if (uMatch && uMatch.length > 1) {
ffVersion = uMatch[1];
}
如果不是Firefox浏览器,ffVersion将未定义。
参见工作示例→
<script type="text/javascript">
var version = navigator.appVersion;
alert(version);
</script>