1

我想检测 IE9 中的兼容模式并向用户显示警告消息。

我的兼容性视图设置是“以兼容模式显示所有网站”。但是,对于 IE9,这种情况仍然不成立。

if(navigator.userAgent.indexOf("MSIE 7.0") > 0 && navigator.userAgent.indexOf("Trident/5.0") > 0).

我可以从 F12 选项验证我处于 IE 9 兼容模式。当我打印 navigator.userAgent 时,它显示 MSIE 9.0。

但是,从 F12 选项,如果我从兼容模式更改为正常模式,然后是兼容模式,我可以看到 userAget 具有 MSIE 7.0 并且条件变为 true。

如何解决?

如果我们在 IE9 兼容模式下打开它,互联网上是否有任何网站显示这样的警告?

4

1 回答 1

0

使用 XPath API、用户代理子字符串和窗口属性的组合测试来识别 IE9 兼容模式:

//IE XPath API
if (!!window.XPathEvaluator === false && !!window.XPathExpression === false)
  {
  //Compatibility Mode
  if(/compatible/.test(navigator.userAgent) )
    {
    //IE9 innerWidth property exists, but IE10 matchMedia property does not
    if(!!window.innerWidth && !window.matchMedia)
      {
      alert("IE9 compatibility mode");
      }
    }
  }
于 2013-08-27T00:15:13.163 回答