Search code examples
jqueryinternet-explorerbrowser-support

Detect older IE versions


I need to detect if the user is running an older version of IE (IE9 is fine) from a jQuery plugin, so I won't have control over the HTML.

We've been discouraged from parsing the user-agent string and using $.browser.msie. The $.support method doesn't cover the problem either.

So, I figured out that this works, but I'm not sure if it is "good practice".

$('body').append('<!--[if lte IE 8]><script>$("body").addClass("oldie");</script><![endif]-->');
var old_ie = $('body').is('.oldie');

Would you use this method or stick with parsing the user-agent string (I'll need to figure out if it's IE and get the version number)?


Solution

  • You can run this

    var ie = (function () {
        var undef, v = 3, div = document.createElement('div');
    
        while (
            div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
            div.getElementsByTagName('i')[0]
        );
    
        return v > 4 ? v : undef;
    }());
    

    to detect the version of IE.

    Source: http://ajaxian.com/archives/attack-of-the-ie-conditional-comment

    And then

    if ( ie < 9 ) {
        // do your stuff, for instance:
        window.location = 'http://getfirefox.com'; // :p
    }