How can I detect if IE8 is running in compatibility view?

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.

Answers


In Javascript, use document.documentMode

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


Evidently IE8 has some new properties

document.documentMode

and

document.compatMode

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


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


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.


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...");  
         }
} 

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


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

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.


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

Need Your Help

Convert WPF (XAML) Control to XPS Document

c# wpf xaml xps xpsviewer

Can I take an Existing WPF (XAML) Control, databind it and turn it into an XPS document that can be displayed and printed using the WPF XPS Document Viewer?

Setting a JPA timestamp column to be generated by the database?

java jpa persistence annotations timestamp

In my SQL Server 2000 database, I have a timestamp (in function not in data type) column of type DATETIME named lastTouched set to getdate() as its default value/binding.