我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。
如何检测这样的浏览器版本?
当前回答
为此,您需要检查navigator的值。appVersion或navigator.userAgent 尝试使用:
console.log(navigator.appVersion)
其他回答
<script type="text/javascript">
var version = navigator.appVersion;
alert(version);
</script>
您可以看到浏览器显示的内容,并使用该信息记录或测试多个浏览器。
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`
浏览器JavaScript库提供了此功能。
if (bowser.msie && bowser.version <= 6) {
alert('Hello China');
}
它似乎保养得很好。
我想分享我为我必须解决的问题而编写的代码。它在大多数主流浏览器中都进行了测试,对我来说,它就像一个魅力!
似乎这段代码与其他答案非常相似,但它进行了修改,以便我可以使用它而不是我最近错过的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
我用它来获得实际浏览器版本的名称和编号(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);歌剧;边缘
给我。