Jquery 检查浏览器是否为 IE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15056620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery check if browser is IE
提问by Moussa Harajli
How would i check if the users browser is IE? i have this code here but it is not working.
我将如何检查用户浏览器是否为 IE?我这里有这个代码,但它不起作用。
if($.browser.msie && $.browser.version <= 9)
{
alert('You Are Using An Outdated Browser! Switch To Chrome Or FireFox.');
}
回答by Mottie
Try this solutionby James Padolsey:
试试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.
评论中还有其他有趣的解决方案。
回答by gdoron is supporting Monica
browser
was removed in 1.9.:
browser
在 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.
描述:包含用户代理的标志,从 navigator.userAgent 读取。我们建议不要使用此属性;请尝试改用功能检测(请参阅 jQuery.support)。jQuery.browser 可能会在 jQuery 的未来版本中移动到插件中。
回答by dherman
Test for features, not browsers. If you use and require FormData like you've stated in your comments, then change your check to:
测试功能,而不是浏览器。如果您使用并需要 FormData 就像您在评论中所说的那样,请将您的支票更改为:
if ( !("FormData" in window) ) {
// Tell the user to use a better browser, or whatever
}
回答by Serj Sagan
How about the MS recommended way: http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx
MS推荐的方式如何: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?)
(当然 UserAgent 可以被欺骗……但是,如果有人欺骗他们的 UserAgent,您真的关心他们在您的网站上看到的内容吗?)
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. );
}
return rv;
}
回答by numediaweb
Just a reminder (even thought not directly answers the question); it is always better to user feature detection as opposedto browser detection.
只是一个提醒(甚至认为不能直接回答问题);与浏览器检测相比,用户特征检测总是更好。
This is why Modernizr is there for;
这就是 Modernizr 存在的原因;
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.
为什么使用Modernizr?
利用很酷的新 Web 技术非常有趣,直到您必须支持落后的浏览器。Modernizr 使您可以轻松编写条件 JavaScript 和 CSS 来处理每种情况,无论浏览器是否支持某个功能。它非常适合轻松进行渐进增强。
So you would use it like;
所以你会像这样使用它;
Modernizr.canvas ? showGraph() : showTable();
Modernizr.canvas ? showGraph() : showTable();