JavaScript:检测 IE 的最佳方法

发布于 2024-12-08 11:47:43 字数 302 浏览 1 评论 0原文

阅读这篇文章我发现了以下一段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和我恋爱吧 2024-12-15 11:47:43

您可以通过以下方式检查 IE 引擎 Trident:

var trident = !!window.ActiveXObject;

MSDN 仅在 IE 中支持。

编辑:

注意:上面的代码在 IE-11 中返回 false,如果您还想检测 IE-11,请使用以下代码:

var isIE = "ActiveXObject" in window; //window.ActiveXObject !== undefined;

You can check for Trident, IE's engine, by the following:

var trident = !!window.ActiveXObject;

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:

var isIE = "ActiveXObject" in window; //window.ActiveXObject !== undefined;
戏蝶舞 2024-12-15 11:47:43

如果可以避免,请不要测试浏览器。进行特征检测。这意味着您的代码(更有可能)是面向未来的。在这种情况下,例如,如果您发现浏览器是 IE 并因此决定使用 attachEvent,您就会错过 addEventListener (优越)这一事实在 IE9 中可用。

在这种情况下,测试一下 document.addEventListener 是否存在。如果是的话,你就有答案了。

if (document.addEventListener) {
    document.addEventListener(...);
} else {
    document.attachEvent(...);
}

编辑:上面 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 that addEventListener (superior) is available in IE9.

In this case, test to see if document.addEventListener exists. If it does, you have the answer.

if (document.addEventListener) {
    document.addEventListener(...);
} else {
    document.attachEvent(...);
}

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.

木槿暧夏七纪年 2024-12-15 11:47:43

要检查浏览器是否为 Internet Explorer,请使用功能检测来检查 documentMode

http://msdn.microsoft.com/en-us/library/ie/cc196988%28v=vs.85%29.aspx

此代码检查浏览器是否为 Internet Explorer 8、9、10 或 11:

var docMode = document.documentMode,
    hasDocumentMode = (docMode !== undefined), 
    isIE8 = (docMode === 8),
    isIE9 = (docMode === 9),
    isIE10 = (docMode === 10),
    isIE11 = (docMode === 11),
    isMsEdge = window.navigator.userAgent.indexOf("Edge/") > -1;

// browser is IE
if(hasDocumentMode) {
     if(isIE11){
         // browser is IE11
     } else if(isIE10){
         // browser is IE10
     } else if(isIE9){
         // browser is IE9
     } else if(isIE8){
         // browser is IE8
     }
} else {
   // document.documentMode is deprecated in MS Edge
   if(isMsEdge){
         // browser is MS Edge
   }
}

检查 document.documentMode 仅在 IE8 到 IE11 中有效,因为 documentMode 是在IE8 并已在 MS Edge 中弃用/删除。

http://msdn.microsoft.com/en -us/library/ff406036%28v=vs.85%29.aspx

我希望这有帮助!

更新

如果你确实需要检测IE7,请检查document.attachEvent

var isIE7 = (document.attachEvent !== undefined);
if(isIE7) {
      // browser is IE7
}

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:

var docMode = document.documentMode,
    hasDocumentMode = (docMode !== undefined), 
    isIE8 = (docMode === 8),
    isIE9 = (docMode === 9),
    isIE10 = (docMode === 10),
    isIE11 = (docMode === 11),
    isMsEdge = window.navigator.userAgent.indexOf("Edge/") > -1;

// browser is IE
if(hasDocumentMode) {
     if(isIE11){
         // browser is IE11
     } else if(isIE10){
         // browser is IE10
     } else if(isIE9){
         // browser is IE9
     } else if(isIE8){
         // browser is IE8
     }
} else {
   // document.documentMode is deprecated in MS Edge
   if(isMsEdge){
         // browser is MS Edge
   }
}

Checking document.documentMode will only work in IE8 through IE11, since documentMode 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:

var isIE7 = (document.attachEvent !== undefined);
if(isIE7) {
      // browser is IE7
}

IE7 returns a object, but if the browser is IE11 (for example), then this would come back as undefined, since IE11 does not have attachEvent.

UPDATE:

Added check for MS Edge. document.documentMode was deprecated in MS Edge. Due to the nature of MS Edge, you can check for Edge/ in the User Agent. Microsoft is making it difficult to use feature detection in MS Edge.

无人问我粥可暖 2024-12-15 11:47:43

IE11 及更早版本不支持 JavaScript includes() 方法。所以你可以用代码来检查是否支持includes()方法。这适用于所有版本的 IE。但 include 方法不适用于早期版本的 Chrome、Firefox、Safari 和 Opera。这可能不是检测 IE 的最有效方法。

var aString = "something";
if(!aString.includes){
    alert("You are using IE");
} else {
    alert("You are not using IE");
}

The JavaScript includes() method is not supported in IE11 and earlier. So you can use code to check whether if the includes() 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.

var aString = "something";
if(!aString.includes){
    alert("You are using IE");
} else {
    alert("You are not using IE");
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文