27

Is there anyway to determine if an IE8 browser has compatibility view switched on?

I can't seem to find anything on Google, and so I'm wondering if this is a piece of information that is available...

Why you ask!? I'm trying to track down a bug in our application. I'm piecing through the Elmah logs and there seems to be a trend; this error is generally thrown by IE8. I tried to repo the defect in my copy of IE8, but couldn't. I want to narrow down the culprits, and thought this might be a way to do it.

Gavin Miller
  • 43,168
  • 21
  • 122
  • 188

9 Answers9

18

In Javascript, use document.documentMode

See http://msdn.microsoft.com/en-us/library/cc196988%28VS.85%29.aspx for details.

Alohci
  • 78,296
  • 16
  • 112
  • 156
7

Evidently IE8 has some new properties

document.documentMode

and

document.compatMode

http://msdn.microsoft.com/en-us/library/cc196988(VS.85).aspx

Tommy
  • 4,011
  • 9
  • 37
  • 59
  • 2
    compatMode is a legacy property, and isn't very useful here. It tells you whether the page is in quirks mode. – EricLaw Jul 31 '09 at 17:14
3

I'm using:

try{ JSON } catch (e){ alert("Compatibility Mode Detected")  }

The JSON object was defined in IE 8, so in IE 7 or when in Compatibility Mode an error is thrown and caught.

I like that this works every time and it's one line.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
Brian McGinity
  • 5,777
  • 5
  • 36
  • 46
3

Check for the "Trident/4.0" in the userAgent. It should be present for IE-8 only. http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/33e0ed49-11fb-4d91-857c-a35496e90075

Michael Baranov
  • 789
  • 1
  • 7
  • 17
2

To expand on @Tommy's answer, use feature detection (alert is optional of course, I use a nice div or inline message somewhere):

if(typeof document.documentMode !== 'undefined') { // checks if this is IE 8 or >
         if(document.documentMode < 8) {  // check if in compat mode 
             // add code here to inform user that they need to turn off compatiblity view
             alert("Click ALT-T then compat view etc...");  
         }
} 
sijpkes
  • 168
  • 10
0

I belive it is contained in the User Agent string:

"A new tag in the User Agent string allows for detection of clients viewing your site in Compatibility View. This tag is also present in the “normal” Internet Explorer 8 User Agent string."

"In the event that a user selects Compatibility View for your site, you can “bring them back” to your desired layout mode via use of the version targeting tag / HTTP header. A new content value, ‘IE=EmulateIE8’, rounds out the list of supported values and assists in this particular scenario."

More information here: http://blogs.msdn.com/ie/archive/2008/08/27/introducing-compatibility-view.aspx

ericvg
  • 3,907
  • 1
  • 30
  • 35
  • See this answer http://stackoverflow.com/questions/1208837/how-can-i-detect-if-ie8-is-running-in-compatibility-view/1208951#1208951: IE7 produces the same User Agent as IE8 in compatibility mode, so user agent doesn't work – Gavin Miller Jul 30 '09 at 19:55
0

In MVC/ASP Request.Browser.Version returns "7.0" regardless of version when in Compatibility View otherwise it returns the browser version.

You can also use Request.Browser.Browser to check that it's IE

Request.Browser.Version
Request.Browser.Browser
Jim
  • 1,333
  • 1
  • 11
  • 15
-1

Very simple method - press F12 , it will open developer tool bar . After menus you will see the Browser mode. Which will clearly tell that it is in IE8 mode or IE7 compatible mode.

shiv mohan
  • 23
  • 1
-1
if(preg_match('/(?i)msie/', $_SERVER['HTTP_USER_AGENT'])) {
if(preg_match('/(?i)Trident\/5/', $_SERVER['HTTP_USER_AGENT'])) {
    echo "IE9";
}
elseif(preg_match('/(?i)Trident\/4/', $_SERVER['HTTP_USER_AGENT'])) {
    echo "IE8";
}
elseif(!preg_match('/(?i)Trident\/4/', $_SERVER['HTTP_USER_AGENT']) AND preg_match('/(?i)msie 7/', $_SERVER['HTTP_USER_AGENT'])) {
    echo "IE7";
}
elseif(preg_match('/(?i)msie [1-6]/', $_SERVER['HTTP_USER_AGENT'])) {
    echo "IE1 à IE6";
}}
bendac
  • 1