IE11通过javascript检测兼容性视图是否为ON

sle*_*lee 18 javascript ie-compatibility-mode internet-explorer-11

有没有人知道如何通过javascript检查IE 11兼容模式是否为ON?

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

navigator.userAgent
Run Code Online (Sandbox Code Playgroud)

在开发人员工具中,它返回

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)像Gecko

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

兼容("兼容")和浏览器("MSIE")令牌已被删除.

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

jae*_*egs 22

解决了

在我自己寻找这个问题的答案时,我在另一个帖子中找到了来自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;
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

source:检测IE10兼容模式


Gav*_*now 7

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

  • 是的,这似乎部分正确,*但是*我发现可以通过IE 11设置强制域的"兼容模式",然后让Web服务器声明`X-UA-Compatible:IE = edge`.这使得它处于一种奇怪的半兼容模式,可以破坏事物(例如谷歌的Recaptcha).这种特殊情况似乎无法通过用户代理字符串进行检测. (2认同)

小智 7

将此添加到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>
Run Code Online (Sandbox Code Playgroud)

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


Lan*_*ard 0

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

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

要了解有关各个令牌的更多信息,请参阅了解用户代理字符串主题。(它并不完全是最新的,但其中的内容似乎与您的兴趣相关。)

希望这可以帮助...

——兰斯