Create a JavaScript file called iecheck.js and paste the following in the snippet:
function trueOrFalse() { return true; } function IeVersion() { // Set defaults var value = { IsIE: false, TrueVersion: 0, ActingVersion: 0, CompatibilityMode: false }; // Try to find the Trident version number var trident = navigator.userAgent.match(/Trident\/(\d+)/); if (trident) { value.IsIE = true; //Convert from the Trident version number to the IE version number value.TrueVersion = parseInt(trident[1], 10) + 4; } // Try to find the MSIE number var msie = navigator.userAgent.match(/MSIE (\d+)/); if (msie) { value.IsIE = true; // Find the IE version number from the user agent string value.ActingVersion = parseInt(msie[1]); } else { // Must be IE 11 in "edge" mode value.ActingVersion = value.TrueVersion; } // If we have both a Trident and MSIE version number, see if they're different if (value.IsIE && value.TrueVersion > 0 && value.ActingVersion > 0) { // In compatibility mode if the trident number doesn't match up with the MSIE number value.CompatibilityMode = value.TrueVersion != value.ActingVersion; } return value; }
Once you have the JavaScript file then open your page where you are going to check for compatibility for and place a reference to the iecheck.js file in the head tag.
<script src="iecheck.js" type="text/javascript"></script>
After that you have multiple options on how to use the script.
For example, if you would like to check for compatibility and redirect then paste the following snippet inside of the body of your html:
<script type="text/javascript"> // 0 = not in compatibility view, 1 = in compatibility view var ie = IeVersion(); if ((ie.CompatibilityMode == "0") && (ie.IsIE == "1")) { window.location = "redirecttosomewhere.html" } else { // do nothing, is in compatibility view } </script>
Another example would be if you just want to see the results on the page; just place this snippet inside of the body of your html:
<script type="text/javascript"> // <![CDATA[ var ie = IeVersion(); document.write("IsIE: " + ie.IsIE + "</br>"); document.write("TrueVersion: " + ie.TrueVersion + "</br>"); document.write("ActingVersion: " + ie.ActingVersion + "</br>"); document.write("CompatibilityMode: " + ie.CompatibilityMode + "</br>"); // ]]> </script>
Hope this helps. Questions are always welcome.