locked
How to Detect compatibility mode in IE (any version)

    Question

  • How to Detect Is IE running in  compatibility mode (any version)

    or Force fully set  IE in  compatible mode


    • Edited by CuriousPoo Friday, December 09, 2011 9:54 AM
    Friday, December 09, 2011 7:56 AM

Answers

  • Hi CuriousPoo,

    There is no ready-made way to detect this. We can proceed with the othernesses of the value to the navigator.userAgent property under different browse mode:
    http://msdn.microsoft.com/en-us/library/cc197025(v=vs.85).aspx.
    We can get familiar with this property and the Compatibility View mode of IE8 and IE9 via the following two blogs:
    Introducing Compatibility View: http://blogs.msdn.com/b/ie/archive/2008/08/27/introducing-compatibility-view.aspx.
    Introducing IE9's User Agent String: http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx.

    After research, we can find that the values to the User Agent String under different browse modes are:
    IE7: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1;...)
    IE8: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0;...)
    IE8 Compatibility View: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0;...)
    IE9: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
    IE9 Compatibility View: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0;...)

    Note that some irrelevant values in the strings are ignored, we only need to focus on some critical values which can be combined to decide the browse mode, like MSIE version NO. and Trident Mode NO..

    Here comes the final solution:
            var agentStr = navigator.userAgent;
            var mode;
            if (agentStr.indexOf("Trident/5.0") > -1) {
                if (agentStr.indexOf("MSIE 7.0") > -1)
                    mode = "IE9 Compatibility View";
                else
                    mode = "IE9";
            }
            else if (agentStr.indexOf("Trident/4.0") > -1) {
                if (agentStr.indexOf("MSIE 7.0") > -1)
                    mode = "IE8 Compatibility View";
                else
                    mode = "IE8";
            }
            else
                mode = "IE7";
    
            document.title = "Browser Mode:\t" + mode;
            //document.write(navigator.userAgent);

    Have a nice day,
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, December 13, 2011 12:17 PM
    Moderator

All replies

  • Hi CuriousPoo,

    There is no ready-made way to detect this. We can proceed with the othernesses of the value to the navigator.userAgent property under different browse mode:
    http://msdn.microsoft.com/en-us/library/cc197025(v=vs.85).aspx.
    We can get familiar with this property and the Compatibility View mode of IE8 and IE9 via the following two blogs:
    Introducing Compatibility View: http://blogs.msdn.com/b/ie/archive/2008/08/27/introducing-compatibility-view.aspx.
    Introducing IE9's User Agent String: http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx.

    After research, we can find that the values to the User Agent String under different browse modes are:
    IE7: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1;...)
    IE8: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0;...)
    IE8 Compatibility View: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0;...)
    IE9: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
    IE9 Compatibility View: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0;...)

    Note that some irrelevant values in the strings are ignored, we only need to focus on some critical values which can be combined to decide the browse mode, like MSIE version NO. and Trident Mode NO..

    Here comes the final solution:
            var agentStr = navigator.userAgent;
            var mode;
            if (agentStr.indexOf("Trident/5.0") > -1) {
                if (agentStr.indexOf("MSIE 7.0") > -1)
                    mode = "IE9 Compatibility View";
                else
                    mode = "IE9";
            }
            else if (agentStr.indexOf("Trident/4.0") > -1) {
                if (agentStr.indexOf("MSIE 7.0") > -1)
                    mode = "IE8 Compatibility View";
                else
                    mode = "IE8";
            }
            else
                mode = "IE7";
    
            document.title = "Browser Mode:\t" + mode;
            //document.write(navigator.userAgent);

    Have a nice day,
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, December 13, 2011 12:17 PM
    Moderator
  • Included IE 10, ensured properties are not set for non-IE browsers, updated as a JS object.  Example call 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
      }



    • Edited by SySys Thursday, August 29, 2013 9:27 PM
    Thursday, August 29, 2013 8:11 PM
  • I wish everyone on the internet would answer questions the way you do, thanks!
    Thursday, July 30, 2015 7:46 PM
  • Hi,

    This is meant to update the response from Leo Liu as I am surprised Microsoft hasn't updated this response yet. Also, the issue of Request.Browser.Version is not accurate. Opera returns as Chrome, although it is a Chromium build it is not Chrome and I think some users would want to know this. Also, the answer from Leo Liu above is flawed in VB .NET currently the line :

    if (agentStr.indexOf("MSIE 7.0") > -1)

    Returns true no matter what the #.# that follows MSIE, the space between MSIE and the #.# that follows breaks this function, and if MSIE exists this will return true. So a browser with Trident/4.0 with MSIE 8.0 will return with "IE8 Compatibility View" according to Leo Liu's code above. So still you won't know accurately, the actual Browser.Version and whether or not Compatibility View is on/off. The following code will determine any Browser/any Version/and correctly captures if Compatibility View is on or off. Leo Liu is correct in the User Agent string, MSIE 7.0 appears ONLY if IE is using compatibility view settings as these were designed to make IE operate as if it were IE 7 no matter what version it actually is at the core. Trident/ with a # is unique to IE and the number also represents the version of IE that is targeted.

    Trident/7.0 = IE 11 down to Trident/4.0 = IE 8

    If we see Trident we know right away it's IE, we examine the following # to determine what the core version of IE should be, after which if MSIE exists, we check the number that follows to see, does it match the core version we have already flagged? If not check if the number is 7, that would mean that it is the core version already flagged, but with compatibility view settings on. If it is not the same or a 7, we want to grab that number and set it to the version of IE as MSIE existing without compatibility view settings turned on should match the version of IE you are running. If it is not 7 and is different than what was flagged from the Trident value, it represents the current/actual version of IE being used. If none of the Trident or MSIE parts of the string exist we know it is another browser altogether and the code follows to determine the correct browser it actually is. if it can not determine any of the most popular browsers UNKNOWN will be returned to show that code worked, but could not determine the browser still. Also, this code allows new browsers or custom entries you would like to flag to be entered here as well. So long Browser.Version giving us Chrome instead of Opera or IE 9 w/ Compatibility View On when really it's just IE 9.

    Dim mode As String = ""
    Try
        Dim agentStr As String = Request.UserAgent

        If agentStr.IndexOf("Trident/7.0") > -1 Then
            mode = "IE11"
            If agentStr.IndexOf("MSIE") > -1 Then
                If Not agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = mode.Substring(2, 1) Then
                    If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 7 Then
                        mode = "IE11 CIE7"
                    Else
                        If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 1 Then
                            mode = "IE " & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 2)
                        Else
                            mode = "IE " & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1)
                        End If
                    End If
                End If
            End If
        ElseIf agentStr.IndexOf("Trident/6.0") > -1 Then
            mode = "IE10"
            If agentStr.IndexOf("MSIE") > -1 Then
                If Not agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = mode.Substring(2, 1) Then
                    If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 7 Then
                        mode = "IE10 CIE7"
                    Else
                        If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 1 Then
                            mode = "IE " & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 2)
                        Else
                            mode = "IE " & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1)
                        End If
                    End If
                End If
            End If
        ElseIf agentStr.IndexOf("Trident/5.0") > -1 Then
            mode = "IE9"
            If agentStr.IndexOf("MSIE") > -1 Then
                If Not agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = mode.Substring(2, 1) Then
                    If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 7 Then
                        mode = "IE9 CIE7"
                    Else
                        If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 1 Then
                            mode = "IE " & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 2)
                        Else
                            mode = "IE " & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1)
                        End If
                    End If
                End If
            End If
        ElseIf agentStr.IndexOf("Trident/4.0") > -1 Then
            mode = "IE8"
            If agentStr.IndexOf("MSIE") > -1 Then
                If Not agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = mode.Substring(2, 1) Then
                    If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 7 Then
                        mode = "IE8 CIE7"
                    Else
                        If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 1 Then
                            mode = "IE " & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 2)
                        Else
                            mode = "IE " & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1)
                        End If
                    End If
                End If
            End If
        ElseIf agentStr.IndexOf("MSIE") > -1 Then
            If agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1) = 1 Then
                mode = "IE" & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 2)
            Else
                mode = "IE" & agentStr.Substring(agentStr.IndexOf("MSIE") + 5, 1)
            End If
        ElseIf agentStr.IndexOf("OPR") > -1 Then
            mode = "OPERA " & agentStr.Substring(agentStr.IndexOf("OPR") + 4, 2)
        ElseIf agentStr.IndexOf("Chrome") > -1 Then
            mode = "CHROME " & agentStr.Substring(agentStr.IndexOf("Chrome") + 7, 2)
        ElseIf agentStr.IndexOf("Firefox") > -1 Then
            mode = "FIREFOX " & agentStr.Substring(agentStr.IndexOf("Firefox") + 8, 2)
        ElseIf agentStr.IndexOf("Safari") > -1 Then
            mode = "SAFARI " & agentStr.Substring(agentStr.IndexOf("Safari") + 7, 3)
        Else
            mode = "UNKNOWN"
        End If
    Catch ex As Exception
    End Try

    Thank you have a nice day!

    Kyle - Programmer/Analyst (IT)




    Monday, October 12, 2015 3:41 PM