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?
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.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.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: