Jquery check if browser is IE

32,716

Solution 1

Try this solution by James Padolsey:

// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
//     ie === undefined
// If you're in IE (>5) then you can determine which version:
//     ie === 7; // IE7
// Thus, to detect IE:
//     if (ie) {}
// And to detect the version:
//     ie === 6 // IE6
//     ie> 7 // IE8, IE9 ...
//     ie <9 // Anything less than IE9
// ----------------------------------------------------------
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;
}());

There are other interesting solutions in the comments as well.

Solution 2

browser was removed in 1.9.:

Description: Contains flags for the useragent, read from navigator.userAgent. We recommend against using this property; please try to use feature detection instead (see jQuery.support). jQuery.browser may be moved to a plugin in a future release of jQuery.

Solution 3

Test for features, not browsers. If you use and require FormData like you've stated in your comments, then change your check to:

if ( !("FormData" in window) ) {
   // Tell the user to use a better browser, or whatever
}

Solution 4

How about the MS recommended way: http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx

(And of course the UserAgent can be spoofed... but, if someone is spoofing their UserAgent do you really care about what they see on your site?)

 function getInternetExplorerVersion()
 // Returns the version of Internet Explorer or a -1
 // (indicating the use of another browser).
 {
    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

        if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 );
    }
    return rv;
}
Share:
32,716
Moussa Harajli
Author by

Moussa Harajli

Updated on January 11, 2020

Comments

  • Moussa Harajli
    Moussa Harajli over 4 years

    How would i check if the users browser is IE? i have this code here but it is not working.

    if($.browser.msie && $.browser.version <= 9)
    {
        alert('You Are Using An Outdated Browser! Switch To Chrome Or FireFox.');   
    }