简体   繁体   中英

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. 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.

Does anyone know an alternative way to do this?

In IE versions 8-11 You can use document.documentMode . Valid values are 5, 7 (compatibility mode), 8, 9, 10, and 11 (Edge).

  • Setting compatibility mode in the console changes the value directly.

  • Loading a page with a <meta http-equiv tag changes the value

  • Adding a site to compatibility mode in "Tools -> Compatibility View settings" changes the value to 7.

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.

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

This page loaded in IE11 sets documentMode to 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

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} */

function isIECompatibilityMode() {

    var ua = navigator.userAgent;

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

        return false;

    }

    return (ua.indexOf("compatible") != -1); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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