Detect IE8 Compatibility Mode

don changer picture don changer · Aug 25, 2009 · Viewed 44.9k times · Source

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).

Answer

Lachlan Roche picture Lachlan Roche · Aug 31, 2009

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)