Javascript 如何使用 jQuery 检测 IE 8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2202305/
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
How do I detect IE 8 with jQuery?
提问by salmane
I need to detect not only the browser type but version as well using jQuery. Mostly I need to find out if it is IE 8 or not.
我不仅需要检测浏览器类型,还需要使用 jQuery 检测版本。大多数情况下,我需要确定它是否是 IE 8。
I am not sure if I am doing it correctly.
我不确定我是否做对了。
If I do :
如果我做 :
if (jQuery.browser.version >= 8.0) {
dosomething}
I am not sure it will work for version 8.123.45.6 or will it?
我不确定它是否适用于 8.123.45.6 版或适用于版本吗?
Edit: Note that JQuery 2+ has dropped support for IE8 and below, and therefore can no longer be used to detect IE8. If using a current version of JQuery, it is necessary to use a Non-JQuery solution.
编辑:请注意,JQuery 2+ 已放弃对 IE8 及更低版本的支持,因此不能再用于检测 IE8。如果使用当前版本的 JQuery,则必须使用非 JQuery 解决方案。
采纳答案by AndiDog
It is documented in jQuery API Documentation. Check for Internet Explorer with $.browser.msieand then check its version with $.browser.version.
它记录在jQuery API 文档中。使用 来检查 Internet Explorer,$.browser.msie然后使用 来检查其版本$.browser.version。
UPDATE:$.browser removed in jQuery 1.9
The jQuery.browser() method has been deprecated since jQuery 1.3 and is removed in 1.9. If needed, it is available as part of the jQuery Migrate plugin. We recommend using feature detection with a library such as Modernizr.
jQuery.browser() 方法自 jQuery 1.3 起已被弃用,并在 1.9 中删除。如果需要,它可以作为 jQuery Migrate 插件的一部分使用。我们建议将特征检测与 Modernizr 等库结合使用。
回答by meo
I think the best way would be this:
我认为最好的方法是这样的:
From HTML5 boilerplate:
来自 HTML5 样板:
<!--[if lt IE 7]> <html lang="en-us" class="no-js ie6 oldie"> <![endif]-->
<!--[if IE 7]> <html lang="en-us" class="no-js ie7 oldie"> <![endif]-->
<!--[if IE 8]> <html lang="en-us" class="no-js ie8 oldie"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en-us" class="no-js"> <!--<![endif]-->
in JS:
在JS中:
if( $("html").hasClass("ie8") ) { /* do your things */ };
especially since $.browserhas been removed from jQuery 1.9+.
尤其是因为$.browser它已从 jQuery 1.9+ 中删除。
回答by Mithun Sreedharan
This should work for all IE8 minor versions
这应该适用于所有 IE8 次要版本
if ($.browser.msie && parseInt($.browser.version, 10) === 8) {
alert('IE8');
} else {
alert('Non IE8');
}
-- update
- 更新
Please note that $.browseris removed from jQuery 1.9
回答by TheBuzzSaw
Don't forget that you can also use HTML to detect IE8.
不要忘记,您还可以使用 HTML 来检测 IE8。
<!--[if IE 8]>
<script type="text/javascript">
ie = 8;
</script>
<![endif]-->
Having that before all your scripts will let you just check the "ie" variable or whatever.
在你所有的脚本之前拥有它会让你只检查“ie”变量或其他任何东西。
回答by kennebec
document.documentModeis undefined if the browser is not IE8,
如果浏览器不是 IE8,则document.documentMode未定义,
it returns 8 for standards mode and 7 for 'compatable to IE7'
它为标准模式返回 8,为“与 IE7 兼容”返回 7
If it is running as IE7 there are a lot of css and dom features that won't be supported.
如果它作为 IE7 运行,则有很多 css 和 dom 功能将不受支持。
回答by user56reinstatemonica8
Assuming...
假设...
- ...that it's the crunky rendering engineof old versions of IE you're interested in detecting, to make a style look right in old IE (otherwise, use feature detection)
- ...that you can't just add conditional comments to the HTML- e.g. for JS plugins that can be applied to any page (otherwise, just do the trick of conditional classes on
<body>or<html>)
- ...它是您有兴趣检测的旧版本 IE的笨拙渲染引擎,以使样式在旧 IE 中看起来正确(否则,请使用特征检测)
- ...您不能只向 HTML 添加条件注释- 例如对于可以应用于任何页面的 JS 插件(否则,只需在
<body>或上执行条件类的技巧<html>)
...then this is probably the best trick (based on this non-jQuery, slightly less flexible variant). It creates then tests for then removes an appropriate conditional comment.
...那么这可能是最好的技巧(基于这个非 jQuery,稍微不那么灵活的变体)。它创建然后测试然后删除适当的条件注释。
(Conditional comments are ignored in IE10+ 'standards mode' - but that should be fine since IE10+ 'standards mode' doesn't have a crazy rendering engine!)
(条件注释在 IE10+“标准模式”中被忽略——但这应该没问题,因为 IE10+“标准模式”没有疯狂的渲染引擎!)
Drop in this function:
放入这个函数:
function isIE( version, comparison ){
var $div = $('<div style="display:none;"/>');
// Don't chain these, in IE8 chaining stops some versions of jQuery writing the conditional comment properly
$div.appendTo($('body'));
$div.html('<!--[if '+(comparison||'')+' IE '+(version||'')+']><a> </a><![endif]-->');
var ieTest = $div.find('a').length;
$div.remove();
return ieTest;
}
Then use it like this:
然后像这样使用它:
if(isIE()){ /* runs in all versions of IE after 4 before standards-mode 10 */ }
if(isIE(8)){ /* runs in IE8 */ }
if(isIE(9)){ /* runs in IE9 */ }
if(isIE(8,'lte')){ /* runs in IE8 or below */ }
if(isIE(6,'lte')){ /* if you need this, I pity you... */ }
I'd also suggest caching the results of this function so you don't have to repeat it. For example, you could use the string (comparison||'')+' IE '+(version||'')as a key to store and check for the result of this test in an object somewhere.
我还建议缓存这个函数的结果,这样你就不必重复它了。例如,您可以使用字符串(comparison||'')+' IE '+(version||'')作为键在某个对象中存储和检查此测试的结果。
回答by Hanzaplastique
Note:
笔记:
1) $.browser appears to be dropped in jQuery 1.9+ (as noted by Mandeep Jain). It is recommendedto use .supportinstead.
1) $.browser 似乎在 jQuery 1.9+ 中被删除了(Mandeep Jain 指出)。这是推荐使用。支持来代替。
2) $.browser.versioncan return "7" in IE >7 when the browser is in "compatibility" mode.
2)当浏览器处于“兼容”模式时,$.browser.version可以在 IE >7 中返回“7”。
3) As of IE 10, conditional commentswill no longer work.
3) 从 IE 10 开始,条件注释将不再起作用。
4) jQuery 2.0+ will drop support for IE 6/7/8
4) jQuery 2.0+ 将不再支持 IE 6/7/8
5) document.documentMode appears to be defined onlyin Internet Explorer 8+ browsers. The value returned will tell you in what "compatibility" mode Internet Explorer is running. Still not a good solution though.
5) document.documentMode 似乎只在 Internet Explorer 8+ 浏览器中定义。返回的值将告诉您 Internet Explorer 正在以何种“兼容”模式运行。尽管如此,仍然不是一个好的解决方案。
I tried numerous .support() options, but it appears that when an IE browser (9+) is in compatibility mode, it will simply behave like IE 7 ... :(
我尝试了许多 .support() 选项,但似乎当 IE 浏览器(9+)处于兼容模式时,它的行为就像 IE 7 ... :(
So far I only found this to work (kind-a):
到目前为止,我只发现这个工作(kind-a):
(if documentMode is not defined and htmlSerialize and opacity are not supported, then you're very likely looking at IE <8 ...)
(如果没有定义 documentMode 并且不支持 htmlSerialize 和 opacity,那么你很可能在看 IE <8 ...)
if(!document.documentMode && !$.support.htmlSerialize && !$.support.opacity)
{
// IE 6/7 code
}
回答by koubic
If you fiddle with browser versions it leads to no good very often. You don't want to implement it by yourself. But you can Modernizrmade by Paul Irish and other smart folks. It will detect what the browser actually cando and put apropriate classes in <html>element. However with Modernizr, you can test IE version like this:
如果您摆弄浏览器版本,通常会导致不好。你不想自己实现它。但是您可以使用 Paul Irish 和其他聪明人制作的Modernizr。它将检测浏览器实际可以做什么并在<html>元素中放置适当的类。但是使用Modernizr,您可以像这样测试 IE 版本:
$('html.lt-ie9').each() {
// this will execute if browser is IE 8 or less
}
Similary, you can use .lt-ie8, and .lt-ie7.
类似地,您可以使用.lt-ie8, 和.lt-ie7。
回答by jkyle
You should also look at jQuery.support. Feature detection is a lot more reliable than browser detection for coding your functionality (unless you are just trying to log browser versions).
您还应该查看jQuery.support。功能检测比浏览器检测更可靠,用于编码您的功能(除非您只是尝试记录浏览器版本)。
回答by Sanooj
You can easily detect which type and version of the browser, using this jquery
您可以使用此 jquery 轻松检测浏览器的类型和版本
$(document).ready(function()
{
if ( $.browser.msie ){
if($.browser.version == '6.0')
{ $('html').addClass('ie6');
}
else if($.browser.version == '7.0')
{ $('html').addClass('ie7');
}
else if($.browser.version == '8.0')
{ $('html').addClass('ie8');
}
else if($.browser.version == '9.0')
{ $('html').addClass('ie9');
}
}
else if ( $.browser.webkit )
{ $('html').addClass('webkit');
}
else if ( $.browser.mozilla )
{ $('html').addClass('mozilla');
}
else if ( $.browser.opera )
{ $('html').addClass('opera');
}
});

