Jquery检查浏览器是否为IE

Mou*_*jli 8 html javascript css jquery internet-explorer

我如何检查用户浏览器是否为IE?我在这里有这个代码,但它不起作用.

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

Mot*_*tie 16

试试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;
}());
Run Code Online (Sandbox Code Playgroud)

评论中还有其他有趣的解决方案.

  • 微软已经删除了IE10的条件评论.见http://www.sitepoint.com/microsoft-drop-ie10-conditional-comments/ (2认同)

gdo*_*ica 11

browser 在1.9中删除:

描述:包含useragent的标志,从navigator.userAgent读取.我们建议不要使用此属性; 请尝试使用功能检测(请参阅jQuery.support).jQuery.browser可能会在未来的jQuery版本中移动到插件中.


dhe*_*man 6

测试功能,而不是浏览器.如果您使用并要求FormData,就像您在评论中所述,那么请将支票更改为:

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

  • 浏览器检测有时很有用.例如,`window.onhashchange`存在,但不会在IE 7和9上触发. (3认同)