admin管理员组

文章数量:1198394

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?

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?

Share Improve this question asked Oct 7, 2011 at 17:22 DanDan 57.9k43 gold badges122 silver badges162 bronze badges 3
  • ajaxian.com/archives/ievv Supposedly it's valid. – Brad Christie Commented Oct 7, 2011 at 17:25
  • 4 Browser detection is never a good idea. Note that 'v'=='\v' is false in IE9+. – duri Commented Oct 7, 2011 at 17:28
  • 1 You're right, new versions of IE support a lot of standard-compliant features that were supported only by FF/Gecko/Opera before, so you don't need to run a fallback code – Dan Commented Oct 7, 2011 at 17:36
Add a comment  | 

4 Answers 4

Reset to default 12

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;

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.

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.

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");
}

本文标签: internet explorerJavaScript The best way to detect IEStack Overflow