RSS Feed
Sep 24

IE Version Reporting in jQuery

Posted on Wednesday, September 24, 2008 in Code

Lately in the course of programming events, it came to my attention that under some circumstances IE 7 identifies itself as both IE 6 and IE 7 in the same userAgent string. Worse, I discovered to my horror that jQuery reports it as IE 6 if the IE 6 declaration happens to be ordered last. This would never do.

The following code snippet will refine jQuery’s browser detection (tested in 1.2.6, patch submitted for 1.3), and makes sure that if two or more IE version declarations are present in the userAgent string, jQuery reports the highest of them.


// Ensure that we report the highest IE version present in the userAgent string
if ( jQuery.browser.msie )
jQuery.browser.version = (userAgent.match( /ie ([\d.]+)/ig ) || [])
.sort().pop().replace( /[^\d.]/g, ” );

To use it, you can either paste it into your jquery.js just after the browser detection stuff (harder), or include it as a script plugin after your jQuery script tag (easier).

Disclaimer: I’ve done some rudimentary testing, but you’re responsible for testing it in your own web project.

This all comes about for several reasons: the True IE 6 obviously won’t be mentioning IE 7 because it didn’t exist at release time, but the True IE 7 might want to pretend it was IE 6 for compatibility with older pages. There’s also apparently a situation wherein an extra-long Windows registry entry will overwhelm IE 7 to the point where it falls back to reporting itself as IE 6.

If similar situations arise after IE 8 (final) is released, this code should take care of those, too. I say “should,” because I obviously don’t know what IE’s going to do in the future. (Sometimes I don’t even know what IE’s doing right now.)