【问题标题】:Detect IE10 compatibility mode检测IE10兼容模式
【发布时间】:2013-01-25 08:14:25
【问题描述】:

我有一些适用于 IE 10 和其他 IE 版本的特定代码路径。如果 IE10 在兼容模式下运行,则浏览器版本设置为 7.0。无论使用 JavaScript/JQuery 的标准/兼容模式如何,有没有办法检测它是否是 IE 10?

【问题讨论】:

  • 兼容模式的重点是它应该伪装成IE7,所以所有正常的版本检测技巧都会说'IE7'。也就是说,您可以使用一些技巧,这里有几个类似的问题可能已经给出了答案......请参阅stackoverflow.com/questions/1328963/…stackoverflow.com/questions/5825385/… 等。(我知道这些适用于 IE8 和 IE9,但可能那里的一些提示可能会有所帮助?)
  • @Spudley,我猜三叉戟是检测的方法。
  • 出于兴趣,您为什么需要检测兼容模式?您应该能够通过使用适当的元标记来避免让用户在兼容模式下运行,所以我不知道您为什么需要检测它。
  • 是的,我想 UA 字符串将是查看的地方。

标签: javascript jquery html internet-explorer internet-explorer-10


【解决方案1】:

您可以使用navigator.userAgent 字符串来检测这一点,例如

"Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.2;WOW64; 三叉戟/6.0; .NET4.0E; .NET4.0C; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Zune 4.7)"

Trident/6.0 表示 IE10

MSIE 7.0 表示兼容模式

更多详情:https://stackoverflow.com/a/5825518/255654

【讨论】:

  • 如果您通过开发工具将其置于兼容视图中,这似乎是正确的。如果由于“工具”菜单中的“兼容性视图设置”(例如通过域名)而将其放入兼容视图,则它似乎使用普通标准 userAgent。
【解决方案2】:

这应该可以检测 MSIE 的兼容模式。

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>

【讨论】:

    【解决方案3】:

    用户代理字符串中的 Trident 值表示正在运行的 IE 的实际版本。

    【讨论】:

      【解决方案4】:

      这是我在 JQuery 中使用的 .ready

        $(document).ready(function () {
          var iec = new IECompatibility();
          alert('IsIE: ' + iec.IsIE + '\nVersion: ' + iec.Version + '\nCompatability On: ' + iec.IsOn);
        });
      
        function IECompatibility() {
          var agentStr = navigator.userAgent;
          this.IsIE = false;
          this.IsOn = undefined;  //defined only if IE
          this.Version = undefined;
      
          if (agentStr.indexOf("MSIE 7.0") > -1) {
            this.IsIE = true;
            this.IsOn = true;
            if (agentStr.indexOf("Trident/6.0") > -1) {
              this.Version = 'IE10';
            } else if (agentStr.indexOf("Trident/5.0") > -1) {
              this.Version = 'IE9';
            } else if (agentStr.indexOf("Trident/4.0") > -1) {
              this.Version = 'IE8';
            } else {
              this.IsOn = false; // compatability mimics 7, thus not on
              this.Version = 'IE7';
            }
          } //IE 7
        }
      

      本·霍布古德

      【讨论】:

      • 我相信如果在 UA 字符串中检测到兼容模式,您的代码只会设置 IsIE 和 Version。这是故意的吗?
      猜你喜欢
      • 2016-11-09
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 2013-12-01
      相关资源
      最近更新 更多