如何使用JavaScript确定确切的浏览器和版本?
当前回答
有时候我们需要一个简单的方法来检查浏览器是否是IE。它可能是这样的:
var isMSIE = (/trident/i).test(navigator.userAgent);
if(isMSIE)
{
/* do something for ie */
}
else
{
/* do something else */
}
或者简化的siva方法:
if(!!navigator.systemLanguage)
{
/* do something for ie */
}
else
{
/* do something else */
}
MSIE .11契
if( (/trident/i).test(navigator.userAgent) && (/rv:/i).test(navigator.userAgent) )
{
/* do something for ie 11 */
}
其他IE浏览器在其userAgent属性中包含MSIE字符串,并可能被它捕获。
其他回答
var browser = navigator.appName;
var version = navigator.appVersion;
然而,请注意,两者并不一定反映事实。许多浏览器都可以设置为其他浏览器的掩码。例如,你不能总是确定用户是用IE6还是用伪装成IE6的Opera上网。
navigator.saysWho = (() => { const { userAgent } = navigator let match = userAgent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [] let temp if (/trident/i.test(match[1])) { temp = /\brv[ :]+(\d+)/g.exec(userAgent) || [] return `IE ${temp[1] || ''}` } if (match[1] === 'Chrome') { temp = userAgent.match(/\b(OPR|Edge)\/(\d+)/) if (temp !== null) { return temp.slice(1).join(' ').replace('OPR', 'Opera') } temp = userAgent.match(/\b(Edg)\/(\d+)/) if (temp !== null) { return temp.slice(1).join(' ').replace('Edg', 'Edge (Chromium)') } } match = match[2] ? [ match[1], match[2] ] : [ navigator.appName, navigator.appVersion, '-?' ] temp = userAgent.match(/version\/(\d+)/i) if (temp !== null) { match.splice(1, 1, temp[1]) } return match.join(' ') })() console.log(navigator.saysWho) // outputs: `Chrome 89`
顾名思义,这将告诉您浏览器提供的名称和版本号。
当您在多个浏览器上测试新代码时,对测试和错误结果进行排序非常方便。
以下是2016年检测浏览器的方法,包括Microsoft Edge、Safari 10和Blink检测:
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
isFirefox = typeof InstallTrigger !== 'undefined';
// Safari 3.0+
isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
// Internet Explorer 6-11
isIE = /*@cc_on!@*/false || !!document.documentMode;
// Edge 20+
isEdge = !isIE && !!window.StyleMedia;
// Chrome 1+
isChrome = !!window.chrome && !!window.chrome.webstore;
// Blink engine detection
isBlink = (isChrome || isOpera) && !!window.CSS;
这种方法的美妙之处在于它依赖于浏览器引擎属性,因此它甚至涵盖了衍生浏览器,如Yandex或Vivaldi,这些浏览器实际上与它们所使用的引擎的主要浏览器兼容。例外是Opera,它依赖于用户代理嗅探,但是今天(即ver。15岁及以上)甚至Opera本身也只是Blink的一个外壳。
通常最好尽可能避免使用特定于浏览器的代码。JQuery $。属性可用于检测对特定特性的支持,而不是依赖于浏览器名称和版本。
以Opera为例,你可以伪造internet explorer或firefox实例。
JQuery的详细描述。支持可以在这里找到:http://api.jquery.com/jQuery.support/
现在根据jQuery弃用。
我们强烈建议使用外部库,如Modernizr 而不是依赖于jQuery.support中的属性。
在编写网站代码时,我总是确保,像导航这样的基本功能对非js用户也是可以访问的。这可能是讨论的对象,如果主页是针对特殊受众,可以忽略。
由于Internet Explorer 11 (IE11+)出来了,不再使用MSIE的标记名称,我提出了一个旧检测函数的变体:
navigator.sayswho= (function(){
var N= navigator.appName, ua= navigator.userAgent, tem;
// if IE11+
if (new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})").exec(ua) !== null) {
var M= ["Internet Explorer"];
if(M && (tem= ua.match(/rv:([0-9]{1,}[\.0-9]{0,})/))!= null) M[2]= tem[1];
M= M? [M[0], M[2]]: [N, navigator.appVersion,'-?'];
return M;
}
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
return M;
})();