Internet explorer 检测到IE 9兼容模式时显示警告

Internet explorer 检测到IE 9兼容模式时显示警告,internet-explorer,internet-explorer-9,ie-developer-tools,Internet Explorer,Internet Explorer 9,Ie Developer Tools,我想检测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 但是,从

我想检测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,并且条件变为真

如何解决?


如果我们在IE9兼容模式下打开,internet上是否有任何网站会显示此类警告?

使用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");
      }
    }
  }

我能给出的最好答案是停止使用兼容模式。没有充分的理由使用它-为什么要通过删除功能故意破坏浏览器?而且compat模式甚至不擅长完全向后兼容。只需使用X-UA-Compatible meta标记强制IE使用标准模式,并保持不变。顺便提一下当您升级到IE11时,您会发现从开发工具切换到兼容模式的选项已被删除,因为MS希望阻止其使用。