-
-
Save koryovip/e8f5c8301ac767732d05be39d7947aea to your computer and use it in GitHub Desktop.
A sample JavaScript file to detect IE compatibility mode and version. This is not recommended unless absolutely needed as features should be detected instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From this page:
http://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx
When Internet Explorer is in Compatibility Mode (IE >= 8), then it sends a user angent:
... MSIE ${versionEmulated}; Trident/${realVersion} ...
So, by example, in my IE8:
Normal mode
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322; .NET4.0C)
compatibility mode:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322; .NET4.0C)
browscap is detecting IE8 in compatibility mode as IE7, which I think as incorrect.
Some expresions with Trident are misplaced (placed on the section of version indicated by MSIE instead of the section that matches Trident).
Solution: move and change expresions with Trident to the real matching IE.
Trident/4.0 -> Internet Explorer 8
Trident/5.0 -> Internet Explorer 9
Trident/6.0 -> Internet Explorer 10
Trident/7.0 -> Internet Explorer 11