如何使用JavaScript仅针对Internet Explorer 11进行定向?

60

如何使用JavaScript最可靠地只针对IE11?

注意:这实际上只应用于分析或通知用户正在使用的浏览器。对于其他所有情况,都应该使用功能检测。


3
搜索"IE11 user-agent"将会给你博客文章和大家看过的这篇文章:http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/。 - Denys Séguret
@dystroy 那篇博客似乎没有任何检测代码。虽然它有IE11的(当前)UA,但许多人不知道如何测试它。 - dave1010
1
参考一下,这是关于IE10的同样问题:https://dev59.com/Cmkw5IYBdhLWcg3wbqAS - dave1010
IE11有多个用户代理字符串:http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx - EricLaw
我很想知道一开始的错误是什么。 :) - jcreamer898
2
重新开放 - 由于某些原因,过去所有重新开放此问题的投票都已经过期了,而且有很多这样的重新开放投票。不用说,留下这些尖刻评论的人是问题的一部分,选择抱怨而不是实际采取行动来尝试重新开放问题。将来,如果您的问题尽管尽力引起社区的关注,但仍未重新开放,并且您强烈希望重新开放它,请随时将其标记为管理员注意事项,解释您的情况,我们会查看。 - BoltClock
7个回答

91

IE 11的用户代理字符串目前为:

Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

Windows 10示例:

Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko
这意味着你可以简单地测试11.xx版本。
var isIE11 = /Trident.*rv[ :]*11\./.test(navigator.userAgent);

IE10的用户代理是这样的

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

可以肯定的是,现在应该将 Trident/X 视为真正的版本号。


4
Trident指的是MSHTML的版本,rv则表示IE的版本。 - Paul Zahra
5
我正在使用modern.ie虚拟运行IE浏览器11.0.9600.x版本。在该版本中,rv和浏览器版本号之间实际上是冒号而不是空格。因此,我需要使用这个正则表达式:!!navigator.userAgent.match(/Trident.*rv[ :]?11\./) - Kevin Jurkowski
4
在 Windows 7 上,使用 IE 11 ,它会返回 false。 - Gopi
1
我该如何检查“如果是IE11及以上版本”?这个正则表达式可以吗? !!navigator.userAgent.match(/Trident.*rv[ :]?[1-9]{2}\./) 正则表达式不是我的强项,但出于兼容性考虑,我希望代码在if语句中执行,如果是IE12、IE13等版本。 - LeigerGaming
1
请注意:此内容不适用于启用兼容性视图的64位Windows 8.1更新版桌面上的IE11。请参阅http://msdn.microsoft.com/en-us/library/ie/hh869301%28v=vs.85%29.aspx。 - Don
显示剩余6条评论

28

IE11保留了"Trident"在它的UA字符串中,但删除了MSIE。检测浏览器是否为IE11或以上版本(如IE12、IE13等)的简单方法是:

var isAtLeastIE11 = !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));

如果你只需要IE11(且不想让未来的IE版本匹配),请按照以下步骤操作:

var isIE11 = !!(navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/rv[ :]11/));

修订版本号用冒号分隔,因此不是“rv:11”。 - Code Uniquely
谢谢。看起来微软改变了这个。我已经更新了正则表达式。 - dave1010

4
var isIE11 = !!navigator.userAgent.match(/Trident\/7.0; rv 11/);

来源:http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/

Internet Explorer 11不应再被称为“IE”。在此版本中,微软已经开始向用户展示他们所信仰的“互操作性标准”的承诺,这意味着IE成为过去式,我们应该把它叫做“Trident”(IE引擎的名称)或“IE11”。


4
在 Windows 7 上,使用 IE 11 运行时会返回 false。 - Gopi
2
这对我在IE 11上也是错误的。 - Robert
可能是由于正则表达式中的点号需要转义。 - user776686

3
这会将ie设置为IE的版本,如果没有则为0。它适用于1到11版,但如果Microsoft放弃Trident引擎,则可能无法检测到未来的版本。
var ie = 0;
try { ie = navigator.userAgent.match( /(MSIE |Trident.*rv[ :])([0-9]+)/ )[ 2 ]; }
catch(e){}

您可能也会对我在这里提供的更详细的相关答案感兴趣。

这是对我来说最好的小脚本。它针对所有IE版本,但不包括我不需要的Edge。:) 感谢@Beejor! - cbloss793

3

这里有一个可以用来检测任何浏览器的脚本:

<script>

  // Opera
  var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

  // Firefox 1.0+
  var isFirefox = typeof InstallTrigger !== 'undefined';

  // Safari 3.0+ "[object HTMLElementConstructor]" 
  var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);

  // Internet Explorer 6-11
  var isIE = /*@cc_on!@*/false || !!document.documentMode;

  // Edge 20+
  var isEdge = !isIE && !!window.StyleMedia;

  // Chrome 1+
  var isChrome = !!window.chrome && !!window.chrome.webstore;

  // Blink engine detection
  var isBlink = (isChrome || isOpera) && !!window.CSS;

  if (isFirefox==true) {
    alert(isFirefox)
    $('.container-fluid').css({"overflow-y":"auto","height":"150%"});  
  }

</script>

3

我使用以下模式来针对所有IE浏览器。如果您只需要IE 11,则可以将其缩短。

 /msie|trident|edge/g.test(navigator.userAgent.toLowerCase());

祝你好运!

Fredrik


2

试一下这个:

navigator.sayswho= (function(){
   var N= navigator.appName, ua= navigator.userAgent, tem;
   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;
})();

来源于JavaScript中的浏览器检测?

更新至IE=11

使用以下代码:

var isIE11 = navigator.userAgent.match(/Trident\/7.0; rv 11.0/);

请阅读此链接,其中讲解了有关IE(Web浏览器)的各种技术。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接