【问题标题】:IE11 detect whether compatibility view is ON via javascriptIE11通过javascript检测兼容性视图是否打开
【发布时间】:2015-01-12 23:06:33
【问题描述】:

有谁知道当我通过 javascript 在网站上时如何检查 IE 11 兼容模式是否开启?

我将 url 添加到列表兼容性视图设置中。但是当我这样做时

navigator.userAgent

在开发者工具中,它返回

Mozilla/5.0(Windows NT 6.3;WOW64;Trident/7.0;.NET4.0E;.NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; rv:11.0) 喜欢壁虎

查看微软网站 (http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx),它说

兼容(“compatible”)和浏览器(“MSIE”)令牌已 删除。

任何有关通过 javascript 检测页面是否使用兼容性视图的帮助都会非常有帮助。提前致谢。

【问题讨论】:

  • 您查看过document.compatModedocumentMode 吗? (我现在不知道这些是否在 IE 11 中相关/可用。)
  • 请问您为什么想知道用户是否处于兼容模式?
  • 我只是在做一些测试。在以前的 IE 版本中,在 url 栏中会有一个“破碎的页面”图标。但在 IE 11 中,无法判断页面是否使用了兼容视图。
  • @slee 你检查过document.documentMode吗?

标签: javascript internet-explorer-11 ie-compatibility-mode


【解决方案1】:

已解决

在自己寻找这个问题的答案时,我在另一个帖子中从 Nenad Bulatovic 那里找到了这个解决方案,但他的回答没有被标记为正确答案。我在 IE11 中对此进行了测试并降级到 IE5,发现它适用于 IE7-IE11,这很棒。我想在这里分享它以防其他人发现它有用。

iecheck.js

function trueOrFalse() {
    return true;
}

function IeVersion() {
    //Set defaults
    var value = {
        IsIE: false,
        TrueVersion: 0,
        ActingVersion: 0,
        CompatibilityMode: false
    };

    //Try to find the Trident version number
    var trident = navigator.userAgent.match(/Trident\/(\d+)/);
    if (trident) {
        value.IsIE = true;
        //Convert from the Trident version number to the IE version number
        value.TrueVersion = parseInt(trident[1], 10) + 4;
    }

    //Try to find the MSIE number
    var msie = navigator.userAgent.match(/MSIE (\d+)/);
    if (msie) {
        value.IsIE = true;
        //Find the IE version number from the user agent string
        value.ActingVersion = parseInt(msie[1]);
    } else {
        //Must be IE 11 in "edge" mode
        value.ActingVersion = value.TrueVersion;
    }

    //If we have both a Trident and MSIE version number, see if they're different
    if (value.IsIE && value.TrueVersion > 0 && value.ActingVersion > 0) {
        //In compatibility mode if the trident number doesn't match up with the MSIE number
        value.CompatibilityMode = value.TrueVersion != value.ActingVersion;
    }
    return value;
}

iecheck.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Testing IE Compatibility Mode</title>
    <script src="iecheck.js" type="text/javascript"></script>
</head>
<body>
<div id="results">Results: </div>
</br>
<script type="text/javascript">

var ie = IeVersion();

document.write("IsIE: " + ie.IsIE + "</br>");
document.write("TrueVersion: " + ie.TrueVersion + "</br>");
document.write("ActingVersion: " + ie.ActingVersion + "</br>");
document.write("CompatibilityMode: " + ie.CompatibilityMode + "</br>");

</script>
</body>
</html>

来源:Detect IE10 compatibility mode

【讨论】:

  • value.CompatibilityMode = value.TrueVersion != value.ActingVersion; 兼容模式 = 怪癖。这不会检测怪癖。它只告诉 IE 正在运行较低版本的仿真,它本身可以是怪癖或标准模式。
  • 为什么这里的 JavaScript 包含与将近一年前发布的答案内容相同的内容?虽然我承认最好发布代码而不是链接到 git 存储库,但这会引起混淆,因为似乎有两种可能的解决方案。
【解决方案2】:

我写了一个 JavaScript 函数 ie-truth 来做这件事。它的工作原理是,在 IE 11 中,如果打开兼容模式,用户代理字符串将包含 IE 11 (7.0) 的 Trident 版本号以及旧版本 IE 的 MSIE 版本号(例如 IE 的 7.0 7)。这也适用于旧版本 IE 中的兼容模式。

【讨论】:

  • 是的,这似乎部分正确,然而我发现可以通过 IE 11 设置强制域的“兼容模式”,然后拥有 Web 服务器声明X-UA-Compatible: IE=edge。这使它处于一种奇怪的半兼容模式,可能会破坏一些东西(例如 Google 的 Recaptcha)。这种特殊情况似乎无法通过用户代理字符串检测到。
  • @SimonEast,我正在努力寻找一种方法来确保 IE 用户不会破坏 Google 验证码,而不会强迫用户弄乱他们的浏览器设置。我在这里有一个悬而未决的问题,并且会对您可能想出的任何解决方案感兴趣。 (stackoverflow.com/questions/42768017/…)
【解决方案3】:

将此添加到 web.config 文件,应用程序将覆盖用户的设置。

<configuration>
  ...
  <system.webServer>
    ...
    <httpProtocol>
        <customHeaders>
          <clear/>
          <add name="X-UA-Compatible" value="IE=8; IE=9; IE=EDGE" />
        </customHeaders>
    </httpProtocol>
    ...
  </system.webServer>
  ...
</configuration>

将“system.webServer”标签放在 web.config 文件的末尾,就在关闭“configuration”标签之前。此外,您可以通过选择您的网站并单击 HTTP 响应标头图标,在您的网络服务器上的 IIS 中添加 X-UA-Compatible 标记。

【讨论】:

  • 这应该进入 web.config 的哪个部分?另外,它是否强制打开或关闭兼容模式?
  • 强制关闭兼容模式。它去向的层次结构现在在答案中。
【解决方案4】:

一个reliable solution 你可以edit 适用于 Internet Explorer 8 到 11(需要额外的代码来支持

讨厌的副作用(仅当 IE

  • 这将 cause problems//@ sourceMappingURL=xx.js(例如在 jQuery //# 格式的库中)。
  • 显然@cc_on 显着减慢了 js 评估(知道他的东西的保罗爱尔兰人)。

它起作用的基本原因是:

var ieRealVersion = Function('return /*@cc_on @_jscript_version@*/;')();

无论兼容模式如何,都返回 IE 的真实版本。备注:

  • ieRealVersion 在 IE11 模式下未为 IE11 定义,但在这种情况下 document.documentMode == 11。
  • IE8 的 ieRealVersion 为 5.8。
  • ieRealVersion 对于 IE7 是 5.7,对于 IE6 SP3 也是 5.7。但是 documentMode 是未定义的,因此您需要额外的代码来检测实际的浏览器(相当简单,但我不再支持 IE6/7,所以我没有费心编写代码)。
  • 这是一种可靠的嗅探 Internet Explorer 6-10 的方法,我已经使用了多年,没有任何问题(但由于 @cc_on 给其他人带来了问题)。

【讨论】:

    【解决方案5】:

    我建议使用功能检测而不是间接查询浏览器版本。因此,例如,如果您需要 HTML 5 历史 API 功能,请执行以下操作:

    if (window.history && window.history.pushState) {
        console.log('This is a SUPPORTED browser');
    } else {
        console.log('NO! You require a browser that does X, please try installing ...');
    }
    

    【讨论】:

      【解决方案6】:

      根据IE Compatibility Cookbookuser agent 文章,有一种方法可以告诉您启用了兼容模式,但前提是用户代理字符串配合使用。

      具体来说,如果浏览器令牌显示MSIE 7.0 而Trident 令牌显示Trident/7.0,这是一个非常明确的指示。尽管如此,自 IE11 RTM 以来对 UA 字符串的更改表明您不能(也不应该)将其作为未来版本的可预测资源。

      要了解有关各个令牌的更多信息,请参阅Understanding user-agent strings 主题。 (它并不完全是最新的,但似乎与您的兴趣相关。)

      希望这会有所帮助...

      -- 兰斯

      【讨论】:

        【解决方案7】:

        一个简单的解决方案,在控制台中试试:

        if (typeof(window.getSelection) == "undefined") {
           alert("Unsupported browser!");
        } else {
           alert("Supported browser");
        }
        

        将检测任何低于 IE9 的 IE(包括兼容性视图)。 还会根据 caniuse 检测 Chrome 4-14。

        IE 6-8:支持选择事件,但不支持 window.getSelection() 参考:https://caniuse.com/#feat=selection-api

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-20
          • 2017-06-05
          • 2012-04-06
          • 2012-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多