How can I detect if IE8 is running in compatibilit

2020-02-17 10:24发布

问题:

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.

回答1:

In Javascript, use document.documentMode

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



回答2:

Evidently IE8 has some new properties

document.documentMode

and

document.compatMode

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



回答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



回答4:

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.



回答5:

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


回答6:

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



回答7:

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


回答8:

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.



回答9:

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