14

I have some specific code paths for IE 10 and rest of the IE versions. If IE10 is running in compatibility mode, browser version is set to 7.0. Is there a way to detect if it is IE 10 irrespective of the standard/compatibility mode using JavaScript/JQuery?

ABC
  • 695
  • 1
  • 8
  • 18
  • 1
    the whole point of compatibility mode is that it's supposed to be pretending to be IE7, so all the normal version detection tricks will say 'IE7'. That said, there are a few tricks that you can use, and several similar questions here that may already give the answer... See http://stackoverflow.com/questions/1328963/detect-ie8-compatibility-mode, http://stackoverflow.com/questions/5825385/javascript-can-i-detect-ie9-if-its-in-ie7-or-ie8-compatibility-mode, etc. (I know those are for IE8 and IE9, but possibly some of the tips there may help?) – Spudley Feb 09 '13 at 21:20
  • 1
    @Spudley, trident is the way to detect I guess. – ABC Feb 09 '13 at 21:33
  • 1
    out of interest, why are you needing to detect compat mode at all? You should be able to avoid having users run in compat mode by using appropriate meta tags, so I don't know why you'd need to detect it. – Spudley Feb 09 '13 at 21:40
  • yes, I imagine the UA string would be the place to look. – Spudley Feb 09 '13 at 22:02

4 Answers4

21

You can detect this using the navigator.userAgent string, for example

"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Zune 4.7)"

Trident/6.0 means IE10

MSIE 7.0 means compatibility mode

More details: https://stackoverflow.com/a/5825518/255654

Community
  • 1
  • 1
Sergei Grebnov
  • 2,633
  • 18
  • 24
  • 4
    If you put it in compat view via dev tools, this seems true. If it gets put into compat view because of Compatibility View Settings in the Tools menu (for example by domain name), it appears to use the normal standards userAgent. – Craig Celeste Oct 08 '14 at 15:38
12

This should work detect compatibility mode for MSIE.

iecheck.js

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;
}

iecheck.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Testing IE Compatibility Mode</title>
    <script src="iecheck.js" type="text/javascript"></script>
</head>
<body>
<div id="results">Results: </div>
</br>
<script type="text/javascript">

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>
</body>
</html>
Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113
6

Trident value in the user agent string indicates the actual version of IE being run.

ABC
  • 695
  • 1
  • 8
  • 18
5

This is what I use from JQuery .ready

  $(document).ready(function () {
    var iec = new IECompatibility();
    alert('IsIE: ' + iec.IsIE + '\nVersion: ' + iec.Version + '\nCompatability On: ' + iec.IsOn);
  });

  function IECompatibility() {
    var agentStr = navigator.userAgent;
    this.IsIE = false;
    this.IsOn = undefined;  //defined only if IE
    this.Version = undefined;

    if (agentStr.indexOf("MSIE 7.0") > -1) {
      this.IsIE = true;
      this.IsOn = true;
      if (agentStr.indexOf("Trident/6.0") > -1) {
        this.Version = 'IE10';
      } else if (agentStr.indexOf("Trident/5.0") > -1) {
        this.Version = 'IE9';
      } else if (agentStr.indexOf("Trident/4.0") > -1) {
        this.Version = 'IE8';
      } else {
        this.IsOn = false; // compatability mimics 7, thus not on
        this.Version = 'IE7';
      }
    } //IE 7
  }

Ben Hobgood

Ben
  • 242
  • 4
  • 3
  • 1
    I believe your code only sets IsIE and Version if compatibility mode is detected in the UA string. Is that intentional? – ifugu May 18 '15 at 15:49