JavaScript: Can I detect IE9 if it’s in IE7 or IE8 compatibility mode?

Actually the user agent string is different for IE9 when being run in IE7 compatibility mode, so this would be one of the best ways to distinguish between different IE versions.

Introducing IE9’s User Agent String:

Similar to IE8, IE9’s Compatibility
View will map to IE7 Standards Mode,
and IE9’s UA string when in
Compatibility View will be:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)

In Compatibility View, IE9 reports
itself as IE7 through the application
version number (Mozilla/4.0) and
version token (MSIE 7.0). This is done
for compatibility. An incremented
Trident token, from ‘Trident/4.0’ to
‘Trident/5.0’, allows websites to
distinguish between IE9 running in
Compat View and IE8 running in Compat
View
.

(emphasis added by me). So the user agent string is the same as it reports itself being “Mozilla/4.0” and MSIE 7.0, but IE9 will always be Trident/5.0 – no matter whether it says MSIE 7.0, MSIE 8.0 or MSIE 9.0.

Actually you should check out this great compilation: Browser ID (User-Agent) Strings or even better useragentstrings.com

Leave a Comment