javascript 我应该如何检测IE浏览器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12186325/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:31:33  来源:igfitidea点击:

How should I detect the IE browser?

javascriptcross-browserbrowser-feature-detection

提问by jfriend00

I've already done feature detection on a particular feature to decide if I can use it or whether I have to use a work-around. But, unfortunately, I found that IE has some bugs in that feature that render it useless to me even when it's present. So, even though it passes the feature detection, I need to detect that the browser is IE so I don't use that feature if it's IE.

我已经对特定功能进行了功能检测,以确定是否可以使用它或是否必须使用变通方法。但是,不幸的是,我发现 IE 在该功能中存在一些错误,即使它存在,也使其对我毫无用处。因此,即使它通过了功能检测,我也需要检测浏览器是 IE,所以如果是 IE,我不会使用该功能。

I've tried to see if I could do feature detection on the actual buggy behavior (which would be best because it would adapt if IE fixes the behavior in the future), but there does not appear to be any way to do that (subject of a different question). That means I'm left with trying to conclusively identify that it's an IE browser so I can avoid the buggy feature. I do not want to use the useragent stringsince we all know that it is easily spoofed and can easily be wrong.

我试图看看我是否可以对实际的错误行为进行特征检测(这将是最好的,因为如果 IE 将来修复该行为,它会适应),但似乎没有任何方法可以做到这一点(主题一个不同的问题)。这意味着我不得不尝试最终确定它是一个 IE 浏览器,这样我就可以避免这个有缺陷的功能。 我不想使用 useragent 字符串,因为我们都知道它很容易被欺骗并且很容易出错。

So, I'd like to do feature detection on whether it's the IE browser or not.

所以,我想对是否是IE浏览器进行特征检测。

The approach I have so far is this:

到目前为止我的方法是这样的:

var isIE = (document.body.attachEvent && window.ActiveXObject);

This looks to see if the attachEvent()method is present and if ActiveXObjectsupport is present. I had to do more than just the attachEventcheck because Opera supports attachEvent.

这将查看该attachEvent()方法是否存在以及是否ActiveXObject存在支持。我必须做的不仅仅是attachEvent检查,因为 Opera 支持attachEvent.

Is this a reliable way to detect IE? Are there better ways?

这是检测 IE 的可靠方法吗?有更好的方法吗?

Please don't respond with I really shouldn't do browser detection, but should use feature detection instead. I'm already doing feature detection, but that caused a problem because a browser that "supports" the feature and thus passes feature detection turns out to be so buggy that I can't use the feature in that browser. So, now I must detect the buggy browser.

请不要回复我真的不应该做浏览器检测,而是应该使用功能检测。我已经在进行功能检测,但这导致了一个问题,因为“支持”该功能并因此通过功能检测的浏览器结果证明是错误的,以至于我无法在该浏览器中使用该功能。所以,现在我必须检测有问题的浏览器。

If you want to know more about the specific situation this is being used in, it's described in this other question.

如果您想了解更多有关正在使用的特定情况的信息,在另一个问题对此进行了描述。

采纳答案by Jeremy J Starcher

Conditional comments conditional compilation, explained here:

条件注释条件编译,解释如下:

http://www.quirksmode.org/css/condcom.html

http://www.quirksmode.org/css/condcom.html

Or the related cousin found here:

或者在这里找到的相关表亲:

http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

Both are Internet Explorer exclusive, so they will allow you to sniff IE.

两者都是 Internet Explorer 独有的,因此它们可以让您嗅探 IE。

<script type="text/javascript">

/*@cc_on
document.write("JScript version: " + @_jscript_version + ".<br>");
   /*@if (@_jscript_version >= 5)
      document.write("JScript Version 5.0 or better.<br \/>");
      document.write("This text is only seen by Internet Explorer browsers that support JScript 5+<br>");
   @else @*/
      document.write("This text is seen by all other browsers (ie: Firefox, IE 4.x etc)<br>");
   /*@end
@*/

</script>

回答by S. Albano

In the linked post, the suggestion of conditional comments was given. This will allow you to detect even the version of IE.

在链接的帖子中,给出了有条件评论的建议。这将使您甚至可以检测 IE 的版本。

Add something like this in your head:

在你的脑海中添加这样的东西:

<!--[if lt IE 9]>
  <script type="text/javascript">
    var lt9=true;
  </script>
<![endif]-->

This is specific to IE, and will be ignored as a comment in other browsers.

这是特定于 IE 的,在其他浏览器中将作为注释被忽略。

For simple detection use something like this:

对于简单的检测,请使用以下内容:

<!--[if gte IE 6]>
  <script type="text/javascript">
    // this is IE greater than or equal to 6
    var isIE=true;
  </script>
<![endif]-->

回答by Tim Down

Using conditional comments rather than conditional compilation has the advantage of allowing detection of the version of IE (as opposed to JScript version, which is the best you can get from conditional compilation). You can do this from script using innerHTMLas follows. The following example shows how to detect IE 8 or lower:

使用条件注释而不是条件编译的优点是允许检测 IE 的版本(与 JScript 版本相反,JScript 版本是您可以从条件编译中获得的最佳版本)。您可以使用innerHTML以下脚本从脚本中执行此操作。以下示例显示了如何检测 IE 8 或更低版本:

var div = document.createElement("div");
div.innerHTML = "<!--[if lte IE 8]><i></i><![endif]-->";
var isIe8orLower = (div.getElementsByTagName("i").length == 1);

alert("Is IE 8 or lower: " + isIe8orLower);

Be aware that IE 10 and later do not support conditional comments at all. However, conditional compilationcontinues to work and is the next best option, so I'd suggest using it, although, as mentioned above, it is unable to identify an actual version of IE.

请注意,IE 10 及更高版本根本不支持条件注释。但是,条件编译继续工作并且是下一个最佳选择,因此我建议使用它,尽管如上所述,它无法识别 IE 的实际版本。