JavaScript:检测 IE 的最佳方法
阅读这篇文章我发现了以下一段代码:
if ('v'=='\v') { // Note: IE listens on document
document.attachEvent('onstorage', onStorage, false);
}
这个方法是'v'=='\v'
是个好主意吗?这是检测 IE 的最短方法吗?
Reading this article I've found a following piece of code:
if ('v'=='\v') { // Note: IE listens on document
document.attachEvent('onstorage', onStorage, false);
}
Is this method 'v'=='\v'
a great idea? Is this the shortest way to detect IE ever?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过以下方式检查 IE 引擎 Trident:
如 MSDN 仅在 IE 中支持。
编辑:
注意:上面的代码在 IE-11 中返回 false,如果您还想检测 IE-11,请使用以下代码:
You can check for Trident, IE's engine, by the following:
As stated on MSDN it is only supported in IE.
Edit:
Note: above code returns false in IE-11, If you want to detect also IE-11 use this:
如果可以避免,请不要测试浏览器。进行特征检测。这意味着您的代码(更有可能)是面向未来的。在这种情况下,例如,如果您发现浏览器是 IE 并因此决定使用
attachEvent
,您就会错过addEventListener
(优越)这一事实在 IE9 中可用。在这种情况下,测试一下
document.addEventListener
是否存在。如果是的话,你就有答案了。编辑:上面 duri 的评论表明此测试在 IE9 中失败(根据标准),这实际上意味着它是
addEventListener
的完美测试,因为它可以从 IE9 中获得。 但是针对特定功能进行编程仍然比针对特定浏览器进行编程要好得多。If you can avoid it, don't test for browsers. Do feature detection. This will mean that your code is (more likely to be) future-proof. In this case, for instance, if you discovered that the browser was IE and decided to use
attachEvent
because of it, you would miss out on the fact thataddEventListener
(superior) is available in IE9.In this case, test to see if
document.addEventListener
exists. If it does, you have the answer.Edit: duri's comment above shows that this test fails in IE9 (as per standards), which actually means it is a perfect test for
addEventListener
, since that is available from IE9. However it is still far, far better to program for specific functionality, rather than specific browsers.要检查浏览器是否为 Internet Explorer,请使用功能检测来检查
documentMode
:http://msdn.microsoft.com/en-us/library/ie/cc196988%28v=vs.85%29.aspx
此代码检查浏览器是否为 Internet Explorer 8、9、10 或 11:
检查
document.documentMode
仅在 IE8 到 IE11 中有效,因为documentMode
是在IE8 并已在 MS Edge 中弃用/删除。http://msdn.microsoft.com/en -us/library/ff406036%28v=vs.85%29.aspx
我希望这有帮助!
更新
如果你确实需要检测IE7,请检查
document.attachEvent
:IE7返回一个对象,但如果浏览器是IE11(例如),那么这会返回作为
未定义
,因为IE11没有attachEvent
。更新:
添加了对 MS Edge 的检查。
document.documentMode
为 在 MS Edge 中已弃用。由于 MS Edge 的性质,您可以在用户代理中检查Edge/
。 Microsoft 正在加大在 MS Edge 中使用功能检测的难度。To check if the browser is Internet Explorer, use feature detection to check for
documentMode
:http://msdn.microsoft.com/en-us/library/ie/cc196988%28v=vs.85%29.aspx
This code checks to see if the browser is Internet Explorer 8, 9, 10, or 11:
Checking
document.documentMode
will only work in IE8 through IE11, sincedocumentMode
was added in IE8 and has been deprecated / removed in MS Edge.http://msdn.microsoft.com/en-us/library/ff406036%28v=vs.85%29.aspx
I hope this helps!
UPDATE
If you really need to detect IE7, check for
document.attachEvent
:IE7 returns a object, but if the browser is IE11 (for example), then this would come back as
undefined
, since IE11 does not haveattachEvent
.UPDATE:
Added check for MS Edge.
document.documentMode
was deprecated in MS Edge. Due to the nature of MS Edge, you can check forEdge/
in the User Agent. Microsoft is making it difficult to use feature detection in MS Edge.IE11 及更早版本不支持 JavaScript
includes()
方法。所以你可以用代码来检查是否支持includes()
方法。这适用于所有版本的 IE。但 include 方法不适用于早期版本的 Chrome、Firefox、Safari 和 Opera。这可能不是检测 IE 的最有效方法。The JavaScript
includes()
method is not supported in IE11 and earlier. So you can use code to check whether if theincludes()
method is supported. This can work for all versions of IE. But the includes method isn't for early versions of Chrome, Firefox, Safari, and Opera. This may not be the most efficient way to detect IE.