8

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.');   
}
Moussa Harajli
  • 1,486
  • 5
  • 22
  • 36

5 Answers5

17

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.

Mottie
  • 84,355
  • 30
  • 126
  • 241
11

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.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 1
    So one would use this check for FormData: `var canFormData = (typeof FormData !== 'undefined');`. There are also feature detection libraries such as [Modernizr](http://modernizr.com/). – ComFreek Feb 24 '13 at 21:11
6

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
}
dherman
  • 2,832
  • 20
  • 23
  • This is the most accurate way to test if a browser is capable of doing what you need. You never know when NewBrowserX will come along that may or may not support a feature you need. This will help you to avoid false positives. – cimmanon Feb 24 '13 at 22:22
  • 3
    Browser detection is sometimes useful. For example, `window.onhashchange` exists but doesn't fire on IE 7 and 9. – David Sherret May 20 '13 at 04:28
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;
}
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
0

Just a reminder (even thought not directly answers the question); it is always better to user feature detection as opposed to browser detection.

This is why Modernizr is there for;

Why use Modernizr?

Taking advantage of cool new web technologies is great fun, until you have to support browsers that lag behind. Modernizr makes it easy for you to write conditional JavaScript and CSS to handle each situation, whether a browser supports a feature or not. It’s perfect for doing progressive enhancement easily.

So you would use it like;

Modernizr.canvas ? showGraph() : showTable();

Community
  • 1
  • 1
numediaweb
  • 16,362
  • 12
  • 74
  • 110