Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery check if browser is IE

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.');   
}
like image 689
Moussa Harajli Avatar asked Feb 24 '13 21:02

Moussa Harajli


People also ask

How do I know if my browser is IE?

To detect whether the current browser is Internet Explorer, you can make use of the navigator. userAgent property. The userAgent property returns the value of the user-agent header sent by the browser to the server. It contains information about the name, version, and platform of the browser.

Is browser IE JavaScript?

JavaScript is a browser-based scripting language that is used by web developers to add dynamic interactions and functionalities to web pages. Today, modern web browsers like Internet Explorer 11 have JavaScript enabled by default, allowing users access to enjoy user-interactive experiences on the internet.

How do you know if you have ie11?

Press the Alt key (next to the Spacebar) on the keyboard to open a menu bar. Click Help and select About Internet Explorer. The IE version is displayed in the pop-up window.

How do you check which browser is being used in JavaScript?

How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ? The browser on which the current page is opening can be checked using JavaScript. The userAgent property of the navigator object is used to return the user-agent header string sent by the browser.


4 Answers

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.

like image 151
Mottie Avatar answered Oct 16 '22 07:10

Mottie


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.

like image 41
gdoron is supporting Monica Avatar answered Oct 16 '22 06:10

gdoron is supporting Monica


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
}
like image 22
dherman Avatar answered Oct 16 '22 08:10

dherman


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;
}
like image 28
Serj Sagan Avatar answered Oct 16 '22 08:10

Serj Sagan