在 jQuery 中检测 Internet Explorer 6 或更低版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/566303/
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
Detect Internet Explorer 6 or below in jQuery
提问by ine
I'm new to jquery and was wondering: is a simple way to detect whether a browser is Internet Explorer 6 or below?
我是 jquery 的新手,想知道:是一种检测浏览器是 Internet Explorer 6 还是更低版本的简单方法?
回答by Aamir Afridi
As simple as this:
就这么简单:
if($.browser.msie && $.browser.version=="6.0") alert("Im the annoying IE6");
Update
更新
Please note that $.browseris removed from jQuery 1.9
If you still need to use $.browser in jQuery 1.9 (or other deprecated functions), try jQuery-migrate (https://github.com/jquery/jquery-migrate/- http://code.jquery.com/jquery-migrate-1.2.1.js)
如果您仍然需要在 jQuery 1.9(或其他不推荐使用的函数)中使用 $.browser,请尝试 jQuery-migrate ( https://github.com/jquery/jquery-migrate/- http://code.jquery.com/jquery -migrate-1.2.1.js)
回答by Alister Bulman
You could also ask IE directly.
也可以直接问IE。
<!--[if lte IE 6]>
<script type="text/javascript">
var isRunningIE6OrBelow = true;
</script>
<![endif]-->
回答by Sampson
jQuery checks for features, rather than "browsers". That said, you can use the jQuery.supportmethod to detect what the users browser is capable of.
jQuery 检查功能,而不是“浏览器”。也就是说,您可以使用jQuery.support方法来检测用户浏览器的功能。
Deprecated Methods (Do not use)
不推荐使用的方法(不要使用)
- $.browser
- $.browser.version
- $.boxModel
- $.浏览器
- $.browser.version
- $.boxModel
http://docs.jquery.com/Utilities/jQuery.supportwill give you a summary of which features are supported by which browsers. Taking that data, you'll develop a couple conditional checks to determine if the browser being used is your target browser or not.
http://docs.jquery.com/Utilities/jQuery.support将为您提供哪些浏览器支持哪些功能的摘要。获取这些数据,您将开发一些条件检查,以确定正在使用的浏览器是否是您的目标浏览器。
回答by Mithun Sreedharan
if ($.browser.msie && parseInt($.browser.version, 10) <= 6) {
alert("I'm not dead yet!");
}
-- update
- 更新
Please not that $.browseris removed from jQuery 1.9
回答by Roman
Very nice way to detect IE is:
检测 IE 的非常好的方法是:
if ('v'=='\v') {
welcome to IE ))
}
Unfortunately it can't recognize its version but it's not always nessecary.
不幸的是,它无法识别它的版本,但它并不总是必要的。
回答by Paul Sweatte
If ActiveXObject
exists and XMLHttpRequest
does not, it's IE6:
如果ActiveXObject
存在XMLHttpRequest
但不存在,则为 IE6:
/* IE6 Check */
(!!window.ActiveXObject && !window.XMLHttpRequest) ? true : false;
In IE7, it would be:
在 IE7 中,它将是:
(!!window.ActiveXObject && !!window.XMLHttpRequest) ? true: false;
References
参考
回答by Paul Deen
I often check the version of a browser. The .support method is great, but it doesn't really help when you need to hide selects when there's an overlay. There is no "supports selects are windowed controls". You just need to check the browser version, so I would say err towards the .support method where you can, and use the .browser where necessary.
我经常检查浏览器的版本。.support 方法很棒,但是当您需要在有叠加层时隐藏选择时,它并没有真正的帮助。没有“支持选择是窗口控件”。你只需要检查浏览器版本,所以我会尽可能对 .support 方法说错误,并在必要时使用 .browser 。
回答by Paul Deen
Also, another good point is that with JQuery, you are not supposed to worry about the version.
此外,另一个好处是,使用 JQuery,您不必担心版本。
That doesn't help if you're using jquery to fix an IE6 css rendering bug though.
如果您使用 jquery 修复 IE6 css 呈现错误,那将无济于事。
回答by James Ellis-Jones
Just to let people know $.browser.msie && /6.0/.test(navigator.userAgent) is not reliable, I've come to this question looking for an answer to a problem with the JQuery bgiframe which breaks on my machine because I have 'Media Center PC 6.0' in my navigator.userAgent string. I've now edited the source to use the $browser.version test.
只是为了让人们知道 $.browser.msie && /6.0/.test(navigator.userAgent) 不可靠,我来到这个问题是为了寻找 JQuery bgiframe 问题的答案,该问题在我的机器上损坏,因为我在我的 navigator.userAgent 字符串中有“Media Center PC 6.0”。我现在已经编辑了源代码以使用 $browser.version 测试。
Its silly of JQuery to deprecate these browser tests because ugly as they may be they are necessary to deal with the ugly state of the browser ecosystem.
JQuery 弃用这些浏览器测试是愚蠢的,因为它们可能是丑陋的,因为它们可能是处理浏览器生态系统丑陋状态所必需的。