Jquery check if browser is IE

2019-03-20 01:16发布

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.');   
}

5条回答
Juvenile、少年°
2楼-- · 2019-03-20 01:35

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.

查看更多
叼着烟拽天下
3楼-- · 2019-03-20 01:38

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;
}
查看更多
SAY GOODBYE
4楼-- · 2019-03-20 01:41

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();

查看更多
萌系小妹纸
5楼-- · 2019-03-20 01:48

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
}
查看更多
狗以群分
6楼-- · 2019-03-20 01:53

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.

查看更多
登录 后发表回答