在没有库的情况下在 JavaScript 中检查 IE 小于 9 的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5574842/
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
Best way to check for IE less than 9 in JavaScript without library
提问by bcm
What would be your fastest, shortest (best) way to detect browser which is IE and version less than 9 in JavaScript, without using jQuery or any add-on libraries?
在不使用 jQuery 或任何附加库的情况下,检测 IE 和 JavaScript 版本低于 9 的浏览器的最快、最短(最佳)方法是什么?
回答by Mike Lewis
Javascript
Javascript
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : undef;
}());
You can then do:
然后你可以这样做:
ie < 9
By James Panolsey from here: http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments
作者 James Panolsey 从这里:http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments
回答by vector
for what it's worth:
物有所值:
if( document.addEventListener ){
alert("you got IE9 or greater");
}
This successfully targets IE 9+ because the addEventListener
method was supported very early on for every major browser but IE. (Chrome, Firefox, Opera, and Safari) MDN Reference. It is supported currently in IE9 and we can expect it to continue to be supported here on out.
这成功地针对 IE 9+,因为该addEventListener
方法很早就被除 IE 之外的每个主要浏览器支持。(Chrome、Firefox、Opera 和 Safari)MDN 参考。它目前在 IE9 中得到支持,我们可以期待它在这里继续得到支持。
回答by Yahel
Using conditional comments, you can create a script block that will only get executed in IE less than 9.
使用条件注释,您可以创建一个仅在 IE 小于 9 中执行的脚本块。
<!--[if lt IE 9 ]>
<script>
var is_ie_lt9 = true;
</script>
<![endif]-->
Of course, you could precede this block with a universal block that declares var is_ie_lt9=false
, which this would override for IE less than 9. (In that case, you'd want to remove the var
declaration, as it would be repetitive).
当然,您可以在此块之前添加一个声明 的通用块var is_ie_lt9=false
,这对于小于 9 的 IE 将覆盖。(在这种情况下,您需要删除var
声明,因为它会重复)。
EDIT: Here's a version that doesn't rely on in-line script blocks (can be run from an external file), but doesn't use user agent sniffing:
编辑:这是一个不依赖于内嵌脚本块的版本(可以从外部文件运行),但不使用用户代理嗅探:
Via @cowboy:
通过@cowboy:
with(document.createElement("b")){id=4;while(innerHTML="<!--[if gt IE "+ ++id+"]>1<![endif]-->",innerHTML>0);var ie=id>5?+id:0}
回答by Mark Kahn
bah to conditional comments! Conditional code all the way!!! (silly IE)
bah 有条件的评论!一路条件码!!!(愚蠢的IE)
<script type="text/javascript">
/*@cc_on
var IE_LT_9 = (@_jscript_version < 9);
@*/
</script>
Seriously though, just throwing this out there in case it suits you better... they're the same thing, this can just be in a .js file instead of inline HTML
说真的,只是把它扔在那里,以防它更适合你……它们是一样的,这可以放在 .js 文件中,而不是内联 HTML
Note: it is entirely coincidental that the jscript_version check is "9" here. Setting it to 8, 7, etc will NOT check "is IE8", you'd need to lookup the jscript versions for those browsers.
注意:此处 jscript_version 检查为“9”完全是巧合。将其设置为 8、7 等不会检查“是 IE8”,您需要查找这些浏览器的 jscript 版本。
回答by execution
Below is an improvement over James Padolsey's solution:
以下是对 James Padolsey 解决方案的改进:
1) It doesn't pollute memory (James' snippet creates 7 unremoved document fragments when detecting IE11, for example).
2) It's faster since it checks for a documentMode value before generating markup.
3) It's far more legible, especially to beginning JavaScript programmers.
1) 它不会污染内存(例如,James 的代码片段在检测 IE11 时创建了 7 个未删除的文档片段)。
2) 它更快,因为它在生成标记之前检查 documentMode 值。
3) 它更易读,尤其是对于 JavaScript 初学者。
Gist link: https://gist.github.com/julianshapiro/9098609
要点链接:https: //gist.github.com/julianshapiro/9098609
/*
- Behavior: For IE8+, we detect the documentMode value provided by Microsoft.
- Behavior: For <IE8, we inject conditional comments until we detect a match.
- Results: In IE, the version is returned. In other browsers, false is returned.
- Tip: To check for a range of IE versions, use if (!IE || IE < MAX_VERSION)...
*/
var IE = (function() {
if (document.documentMode) {
return document.documentMode;
} else {
for (var i = 7; i > 0; i--) {
var div = document.createElement("div");
div.innerHTML = "<!--[if IE " + i + "]><span></span><![endif]-->";
if (div.getElementsByTagName("span").length) {
return i;
}
}
}
return undefined;
})();
回答by Aleko
var ie = !-[1,]; // true if IE less than 9
This hack is supported in ie5,6,7,8. It is fixed in ie9+ (so it suits demands of this question). This hack works in all IE compatibility modes.
ie5,6,7,8 支持此 hack。它在 ie9+ 中是固定的(所以它适合这个问题的要求)。此 hack 适用于所有 IE 兼容模式。
How it works: ie engine treat array with empty element (like this [,1]) as array with two elements, instead other browsers think that there is only one element. So when we convert this array to number with + operator we do something like that: (',1' in ie / '1' in others)*1 and we get NaN in ie and 1 in others. Than we transform it to boolean and reverse value with !. Simple. By the way we can use shorter version without ! sign, but value will be reversed.
它是如何工作的:即引擎将带有空元素的数组(如 [,1])视为带有两个元素的数组,而其他浏览器认为只有一个元素。因此,当我们使用 + 运算符将这个数组转换为数字时,我们会执行以下操作:(',1' in ie / '1' in others)*1 并且我们在 ie 中得到 NaN,在其他中得到 1。然后我们将其转换为布尔值并使用 ! 反转值。简单的。顺便说一句,我们可以使用更短的版本而没有 ! 符号,但值将反转。
This is the shortest hack by now. And I am the author ;)
这是目前最短的 hack。我是作者;)
回答by bcm
I've decided to go with object detection instead.
我决定改用对象检测。
After reading this: http://www.quirksmode.org/js/support.htmland this: http://diveintohtml5.ep.io/detect.html#canvas
阅读本文后: http: //www.quirksmode.org/js/support.html和:http: //diveintohtml5.ep.io/detect.html#canvas
I'd use something like
我会使用类似的东西
if(!!document.createElement('canvas').getContext) alert('what is needed, supported');
回答by Michael Benin
This link contains relevant information on detecting versions of Internet Explorer:
此链接包含有关检测 Internet Explorer 版本的相关信息:
http://tanalin.com/en/articles/ie-version-js/
http://tanalin.com/en/articles/ie-version-js/
Example:
例子:
if (document.all && !document.addEventListener) {
alert('IE8 or older.');
}
回答by Alex
You could do it in a quick and dirty fashion with a regular expression and .match()
:
您可以使用正则表达式以快速而肮脏的方式完成它,并且.match()
:
if (navigator.userAgent.match(/MSIE\s(?!9.0)/)) {
// ie less than version 9
}
回答by Knu
If I were you I would use conditional compilationor feature detection.
Here's another alternative:
如果我是你,我会使用条件编译或特征检测。
这是另一种选择:
<!--[if lt IE 9]><!-->
<script>
var LTEIE8 = true;
</script>
<!--<![endif]-->