如何检查IE11是否在使用JS的兼容性视图中
我想检查是否为当前域启用了IE11兼容性视图。设置兼容性视图是通过:工具>兼容性视图设置。如何检查IE11是否在使用JS的兼容性视图中
我知道这已被要求由少数几个年前,但看起来像answers不工作了,由于recent update on IE11.
有谁知道另一种方式来做到这一点?
如果你只是想检查你是否在兼容模式下运行,你可以使用这个脚本。
//创建新对象ieUserAgent
var ieUserAgent = {
init: function() {
// Get the user agent string
var ua = navigator.userAgent;
this.compatibilityMode = false;
// alert (ua);
if(ua.indexOf("MSIE") == -1){
this.version = 0;
return 0;
}
if(ua.indexOf("compatible") == -1){
this.compatibilityMode = false;
return 0;
}else{
this.compatibilityMode = true;
return 0;
}
}
};
// Initialize the ieUserAgent object
ieUserAgent.init();
-OR-
/** * 检查客户端是IE和兼容视图 * * @Returns {布尔} */
function isIECompatibilityMode() {
var ua = navigator.userAgent;
if (ua.indexOf("MSIE") == -1) {
return false;
}
return (ua.indexOf("compatible") != -1); }
感谢Janaki,但这与旧解决方案相同,我上面提到的IE11更新删除了字符串“MSIE “不管你是否在兼容性视图中,所以这是行不通的。 – Byron
请格式化您的答案。 – 2017-10-17 05:03:45
在IE版本8-11中您可以使用document.documentMode
。有效值为5,7(兼容模式),8,9,10和11(边缘)。
在控制台中设置兼容模式直接更改值。
加载一个网页,一个
<meta http-equiv
标签改变“工具 - >兼容性视图设置 ”值添加网站到兼容模式的值变化为7
https://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx
示例
例如,如果我在IE11加载这个页面,我得到的11
<!doctype HTML>
<body>
<p>Hello World!<p>
</body>
documentMode
此页面加载IE11设置documentMode
到9
<html>
<head>
<meta http-equiv="x-ua-compatible" content="IE=9"/>
</head>
<body>
<p>Hello World!<p>
</body>
</html>
我想检测兼容性视图是否启用兼容模式下的开发工具,我认为你是指仿真选项卡。在IE11设置兼容性视图是通过 - 工具>兼容性视图设置 – Byron
的可能的复制[如何检测IE11?] (https://stackoverflow.com/questions/17907445/how-to-detect-ie11) –
不完全相同。这是如何检查IE 11的兼容模式。以上链接仅用于检查IE 11版本。 – Jimbob
好像你只需要在userAgent的开始处寻找'compatible' –