【问题标题】:How to properly detect IE11 or later?如何正确检测 IE11 或更高版本?
【发布时间】:2015-06-17 07:11:40
【问题描述】:

我正在运行带有 IE 11 的 Windows 7(64 位)

拥有以下navigator.userAgent

"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2;
.NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0;
.NET4.0C; .NET4.0E)"

我希望能够在我的网站上显示任何内容之前检测 IE 的版本。换句话说,我工作的公司已经更新了大多数计算机以运行 IE11 或 Chrome。但是有些电脑还是有IE9的。

我希望我的网站能够为运行 IE11 或 chrome 的用户正常工作。应该检测到任何其他版本的浏览器,并通知用户更新他的机器。

我在 SO 引用 v11 上找到的所有代码都是 userAgent 字符串的一部分,但这里不是这种情况。

编辑:我也试过了:

var isIE11 = !!(navigator.userAgent.match(/Trident/) 
             && !navigator.userAgent.match(/MSIE/));  
         //value is false in IE6/IE9/IE11

var isIE11 = !!(navigator.userAgent.match(/Trident/) 
                 && navigator.userAgent.match(/rv 11/));   
          //value is false in IE6/IE9/IE11

var isIE11 = !(window.ActiveXObject) && "ActiveXObject" in window;
           //value is false in IE6/IE9/IE11

如何检测IE11?

编辑 2:此链接 http://kangax.github.io/compat-table/es6/ 可以检查仅在 IE11 上运行的 hoisted ... 功能。所以我也试过这个:

{ function f() { return 1; } }
  function g() { return 1; }
{ function g() { return 2; } }
{ function h() { return 1; } }
  function h() { return 2; }

alert( f() === 1 && g() === 2 && h() === 1);  // alerts false in all ie versions

【问题讨论】:

  • @Vincent1989 谢谢,但我已经去过那个答案了。我的情况是独一无二的。我拥有的 ie 是 11,但 userAgent 没有说明这一点。这就是为什么它对我不起作用
  • 为什么要检测浏览器类型/版本?推荐的方法是检测功能并仅在可用时实现它们。另见stackoverflow.com/questions/21391593/…
  • @Xotic750 ie11 可以做什么(在 javascript 中)以前版本不能做的事情是什么?
  • 例如IE11+支持hoisted block-level function declaration(chrome目前不支持,firefox支持),可以测试这个功能。

标签: javascript internet-explorer internet-explorer-11


【解决方案1】:

如果只有 IE 浏览器连接到页面,则测试 hoisted block-level function declaration 仅在 IE11+ 中支持

function hoistTest() {
    // Note: only available outside of strict mode.
    { function f() { return 1; } }
      function g() { return 1; }
    { function g() { return 2; } }
    { function h() { return 1; } }
      function h() { return 2; }
    
    return f() === 1 && g() === 2 && h() === 1;
}

document.getElementById('out').appendChild(document.createTextNode('hoisted block-level function declaration: ' + hoistTest()));
<pre id="out"></pre>

更新:它在 IE11 上运行的屏幕截图

【讨论】:

    【解决方案2】:

    这仅在 IE11 中返回 true:

    !(window.ActiveXObject) && "ActiveXObject" in window
    

    【讨论】:

    • 试过了。在我测试过的所有版本的 IE 中都给了false。我更新了我的问题以提供此信息
    【解决方案3】:

    检查 Trident 引擎版本是否为 7.0;较早版本的 Internet Explorer 具有较早的 Trident 引擎版本。

    例子:

    • Internet explorer 10 有 Trident 6.0
    • Internet explorer 9 有 Trident 5.0

    来源:https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx

    请注意,您必须特别注意非桌面 PC Internet Explorer 版本(Lumia 手机、Xbox 等)。

    此外,最新版本的 Microsoft 浏览器 Edge 不再使用 Trident 版本。

    【讨论】:

    • 我的 ie11 的 userAgent 返回一个包含 Trident/7 的字符串,当我再次为 ie7 运行 ie7(我得到 Trident/7)时也会发生这种情况
    【解决方案4】:

    在 IE 中,您可以访问 document 上的 documentMode 属性,这将返回 IE 的版本。

    要测试 11 及更高版本,您可以使用以下内容:

    document.documentMode > 10
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多