Detect IE8 Compatibility Mode [duplicate]

2019-01-06 17:29发布

问题:

Possible Duplicate:
Differentiate IE7 browser and browser in IE7 compatibility mode

We have not had time to fully test IE8 with our fairly extensive system. We are also reaching the point at which we can't prohibit the use of IE8. Our users are stubborn and not terribly sophisticated, so we don't feel that we can tell them to use the "Compatibility Mode" button. Instead, we will add the "IE=EmulateIE7" meta tag as a temporary fix.

The problem is that when this tag is used, the browser still reports itself to be IE8, which is then detected by our pages and rejected. I can understand why server-side detection can't pick up the meta tag (the server already has the user-agent before the meta tag is sent and processed), but client-side checks also fail. If the compatibility mode button is clicked on the client machines, the user-agent is IE7, but not if we use the meta tag.

This means, among other things, that we have to disable the checking and then, if we make a mistake somewhere and forget to add the meta tag to a page, we will have broken pages.

Has anyone else encountered this and come up with a better approach? Is there no way to detect that IE8 is running in compatibility mode when the mode is set by the meta tag?

And, we do know that there is a valid school of thought that we should just make all the pages compatible with IE8 Standards mode but in our little corner of reality, that is not a feasible option right now (and I'll point out that we didn't create 4 or 5 versions of IE that had to be written for in a non-standard way).

回答1:

IE8 includes a trident token in the User-Agent string regardless of compatibility mode.

See MSDN for more details: http://blogs.msdn.com/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx

IE7 on Windows Vista
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)

IE8 on Windows Vista (Compatibility View)
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)

IE8 on Windows Vista
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)



回答2:

You need to check the value of document.documentMode which will have a value of 8 in true IE8 mode and 7 in IE7 mode. The user agent string will be identical in both cases and will still state IE8. Note that this JavaScript variable is only available in IE8.



回答3:

FYI, for details on how to detect Compatibility Mode (AKA how to detect Compatibility View), please consider viewing the following link...

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

...which shows some helpful information.



回答4:

Problem is actually even more complex when you deal with links coming to your site from IE8 emulation BLACKLIST sites such as Facebook.

A shared link on Facebook causes your links to open in a Popup - IF THE "INCLUDE UPDATE WEBLISTS" is checked on the compatibility view settings, which is checked by default, the popup window sees the Blacklist referrer from Facebook and opens the window in Blacklist mode initially. When detecting in background for Trident\4.0 - you will see the value, and set the downlevel CSS.

However, after the browser loads it switches out of Compat mode and changes to STANDARD mode without refreshing. You therefore have the wrong CSS for standard modes assigned.

Solution - Force the IE Browser out of Compatibility mode. Yep - seems odd, why let a user go downlevel on your site if you don't need to.

Just like Downlevel you set IE=EmulateI7 to force the browser down you can add IE=EmulateIE8 and force the user to view your page in IE8 Standard. this mode even removes their ability to select Compatibility View and reduce your Test Needs for that mode



回答5:

Not quite "detecting IE8 compatibility mode" but it might help just as well:

If you have access to the server itself, you can send out an HTTP Header, take a look at Mike Ormond's blog about it. The MSDN page for Implementing the Meta Switch on IIS has details of the header and how to set/send it.