Internet Explorer User-Agent Strings

Internet Explorer can be a right PITA when it wants to be. I’m using Thickbox to render on page dialogues in one of my projects. Thickbox relies on testing $jquery.browser.version for determining the version number of Internet Explorer.

!(jQuery.browser.msie && jQuery.browser.version < 7

Apart from the fact that it doesn’t always work. Below is what you would expect to be presented as a user agent string for IE8:

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB6; SLCC2; 
.NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; 
.NET CLR 4.0.20506; InfoPath.2)

So far today, I’ve seen three customers with the following string

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; 
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; 
.NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 
3.0.4506.2152; .NET CLR 3.5.30729)

Somewhere, somehow, the user agent string has become corrupted (and hence the MSIE 6.0). There’s some information on this available on Microsoft communities.

The way around this is to do some explicit regular expression checking on the useragent string. For those that didn’t know about this previously, it’s demonstrated by Jamie Thompson. He introduces a new property called $.browser.msie6 which is used to check for the presence of the IE6 string without the IE7 string.

$.browser.msie6 =
    $.browser.msie
    && /MSIE 6.0/i.test(window.navigator.userAgent)
    && !/MSIE 7.0/i.test(window.navigator.userAgent);

You then adapt thickbox.js to test for this new property.

if ( !(jQuery.browser.msie6)) { // take away IE6
    $("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
}

Which works great for IE6, great for IE7, fine for vanilla IE8 (which doesn’t suffer from the above bug), but falls down on corrupted IE8. Not to panic, simply adjust $.browser.msie6 to look for this additional string.

$.browser.msie6 =
    $.browser.msie
    && /MSIE 6.0/i.test(window.navigator.userAgent)
    && !/MSIE 7.0/i.test(window.navigator.userAgent)
    && !/MSIE 8.0/i.test(window.navigator.userAgent);

The downside to this fix is that if and when Microsoft comes out with Internet Explorer 9, then you’ll need to adjust this script again to take this into account. I therefore propose a slight departure, using regular expressions to test for values above 6:

$.browser.versionOver6 = function() {
    var re = /MSIE (\d+)/ig;
    var match;
    while (match = re.exec(window.navigator.userAgent)) {
        if (match[1] > 6) {
            return true;
        }
    }
    return false;
}</p>

$.browser.msie6 = $.browser.msie && $.browser.versionOver6(); </pre> This modified version should work on all future versions of IE. Browsers which report a MSIE 6.0 as _well as_ a MSIE 7.0, MSIE 8.0, MSIE 9.0, etc will now also report as not IE6, which should make filtering off IE6 fixes a little easier. Enjoy.