Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A sample JavaScript file to detect IE compatibility mode and version. This is not recommended unless absolutely needed as features should be detected instead.
// Check to see if jQuery is loaded. If not, load it from the public jQuery CDN.
if (typeof jQuery == 'undefined') {
// Load the latest jQuery library from jQuery
document.write("\<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'>\<\/script>");
}
// Create new ieUserAgent object
var ieUserAgent = {
init: function () {
// Get the user agent string
var ua = navigator.userAgent;
this.compatibilityMode = false;
// Detect whether or not the browser is IE
var ieRegex = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (ieRegex.exec(ua) == null)
this.exception = "The user agent detected does not contai Internet Explorer.";
// Get the current "emulated" version of IE
this.renderVersion = parseFloat(RegExp.$1);
this.version = this.renderVersion;
// Check the browser version with the rest of the agent string to detect compatibility mode
if (ua.indexOf("Trident/6.0") > -1) {
if (ua.indexOf("MSIE 7.0") > -1) {
this.compatibilityMode = true;
this.version = 10; // IE 10
}
}
else if (ua.indexOf("Trident/5.0") > -1) {
if (ua.indexOf("MSIE 7.0") > -1) {
this.compatibilityMode = true;
this.version = 9; // IE 9
}
}
else if (ua.indexOf("Trident/4.0") > -1) {
if (ua.indexOf("MSIE 7.0") > -1) {
this.compatibilityMode = true;
this.version = 8; // IE 8
}
}
else if (ua.indexOf("MSIE 7.0") > -1)
this.version = 7; // IE 7
else
this.version = 6; // IE 6
}
};
// Initialize the ieUserAgent object
ieUserAgent.init();
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing IE Compatibility Mode</title>
<script src="ieUserAgent.js" type="text/javascript"></script>
</head>
<body>
<div id="results">Results:</div>
<script type="text/javascript">
var val = "IE" + ieUserAgent.version;
if (ieUserAgent.compatibilityMode)
val += " Compatibility Mode (IE" + ieUserAgent.renderVersion + " emulation)";
$("#results").html("We have detected the following IE browser: " + val);
</script>
</body>
</html>
@wickdninja

This comment has been minimized.

Copy link

commented Apr 10, 2014

"The user agent detected does not contai Internet Explorer."; Missing 'n' on contain

@iwx1

This comment has been minimized.

Copy link

commented Jun 30, 2014

How about IE 11 since the user agent has been changed?
There is no MSIE and it shows something like Gecko.
How to detect whether its IE 11?

@hgc2002

This comment has been minimized.

Copy link

commented Oct 22, 2014

@robertc

This comment has been minimized.

Copy link

commented Mar 19, 2015

This reports IE11 as IE6 whether compatibility view is engaged or not.

@bobotr

This comment has been minimized.

Copy link

commented Aug 3, 2015

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();

@Horlacher

This comment has been minimized.

Copy link

commented May 25, 2016

/**
 * 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);
}
@manassingh2k

This comment has been minimized.

Copy link

commented Sep 19, 2018

Hi Jason,
Actually I am looking out for how to add site URL/domain in IE compatibility Mode through JavaScript.
Or anyway to automate the process instead of manually adding URL entry in IE Compatibility View Settings.

~Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.