[英]How to check if IE11 is in compatibility view using JS
我想检查是否为当前域启用了IE11兼容性视图。 设置兼容性视图是通过:工具>兼容性视图设置。
我知道几年前有人问过这个问题,但由于IE11最近的更新,看起来答案不再适用了。
有没有人知道另一种方法吗?
在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的documentMode
。
<!doctype HTML>
<body>
<p>Hello World!<p>
</body>
在IE11中加载的此页面将documentMode
设置为9。
<html>
<head>
<meta http-equiv="x-ua-compatible" content="IE=9"/>
</head>
<body>
<p>Hello World!<p>
</body>
</html>
如果您只想检查是否在兼容模式下运行,可以使用此脚本。
//创建新的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();
-要么-
/ ** *检查客户端是否是IE并且在兼容性视图中* * @returns {boolean} * /
function isIECompatibilityMode() {
var ua = navigator.userAgent;
if (ua.indexOf("MSIE") == -1) {
return false;
}
return (ua.indexOf("compatible") != -1); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.