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

Byr*_*ron 5 javascript compatibility internet-explorer-11

我想检查当前域是否启用了IE11兼容性视图。可以通过以下方式设置兼容性视图:工具>兼容性视图设置。

我知道几年前就已经提出过这个问题,但是由于IE11的最新更新,答案似乎不再起作用

有谁知道替代方法吗?

Use*_*ser 0

如果您只想检查是否正在兼容模式下运行,可以使用此脚本。

// 创建新的 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();
Run Code Online (Sandbox Code Playgroud)

-或者-

/** * 检查客户端是否为 IE 且处于兼容性视图 * * @returns {boolean} */

function isIECompatibilityMode() {

    var ua = navigator.userAgent;

    if (ua.indexOf("MSIE") == -1) {

        return false;

    }

    return (ua.indexOf("compatible") != -1); }
Run Code Online (Sandbox Code Playgroud)