简体   繁体   English

如何使用JS检查IE11是否处于兼容性视图中

[英]How to check if IE11 is in compatibility view using JS

I'd like to check if IE11 compatibility view is enabled for the current domain. 我想检查是否为当前域启用了IE11兼容性视图。 Setting compatibility view is through: Tools > Compatibility View Settings. 设置兼容性视图是通过:工具>兼容性视图设置。

I know this has been asked by a few a couple of years ago but looks like the answers doesn't work anymore due to recent update on IE11. 我知道几年前有人问过这个问题,但由于IE11最近的更新,看起来答案不再适用了

Does anyone know an alternative way to do this? 有没有人知道另一种方法吗?

In IE versions 8-11 You can use document.documentMode . 在IE版本8-11中您可以使用document.documentMode Valid values are 5, 7 (compatibility mode), 8, 9, 10, and 11 (Edge). 有效值为5,7(兼容模式),8,9,10和11(边缘)。

  • Setting compatibility mode in the console changes the value directly. 在控制台中设置兼容模式会直接更改值。

  • Loading a page with a <meta http-equiv tag changes the value 使用<meta http-equiv标签加载页面会更改该值

  • Adding a site to compatibility mode in "Tools -> Compatibility View settings" changes the value to 7. 在“工具 - >兼容性视图设置”中将站点添加到兼容模式会将值更改为7。

https://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx

Examples 例子

For example if I load this page in IE11 I get documentMode of 11. 例如,如果我在IE11中加载此页面,我得到11的documentMode

<!doctype HTML>
<body>
<p>Hello World!<p>
</body>

This page loaded in IE11 sets documentMode to 9. 在IE11中加载的此页面将documentMode设置为9。

<html>
<head>
<meta http-equiv="x-ua-compatible" content="IE=9"/>
</head>
<body>
<p>Hello World!<p>
</body>
</html>

If you just wanting to check if you are being run in compatibility mode you can use this script. 如果您只想检查是否在兼容模式下运行,可以使用此脚本。

// Create new ieUserAgent object //创建新的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- -要么-

/** * Check if client is IE and in compatibility view * * @returns {boolean} */ / ** *检查客户端是否是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM