Reliable IE 7 detection with JavaScript?
Currently, the most accurate method of detecting Internet Explorer 7 with JavaScript is the following:
var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
There’s an alternate, but in my opinion less future proof, method:
var ie7 = (typeof document.addEventListener != 'function' && window.XMLHttpRequest) ? true : false;
I don’t believe they will ever drop document.all, but I’m certain they are going to fix addEventListener method.
Test conditions
Here’s a quick breakdown of objects/methods tested:
document.all- returns
truein all Internet Explorer versions and also in Opera window.opera- also known as
operaobject, returnstruein Opera window.XMLHttpRequest- returns
truein Firefox, Opera, Safari and IE 7 - returns
falsein IE 6 and lower
So far, I haven’t found flaws in this detection method. However, if someone knows better, please do tell. Ken Snyder pointed out that the native XMLHttpRequest can be disabled in IE 7 by unchecking Tools -> Options -> Advanced -> “Enable native XMLHTTP support” option… Bummer!
Conditional Comments
Last, but not least, you can always set ie7 variable to true inside conditional comments:
<!––[if IE 7]> <script type="text/javascript">ie7 = true;</script> <![endif]––>

8 Comments
You don’t need to use the triadic operator, as your bracketed condition returns a Boolean value. Just use
var ie7 = (document.all && !window.opera && window.XMLHttpRequest);
(although the Conditional Comment method is rather elegant, IMHO.)
Comment (#) by Nick Fitzsimons — 31st October 2006.
Wow, great tip about window.XMLHttpRequest, which I might find a use for, very soon.
Comment (#) by Aleksandar — 31st October 2006.
The best approach is not to test for browsers in general, but to test for only those features you need. If you need document.all, test for it, not for a particular version of IE.
Comment (#) by Berislav Lopac — 1st November 2006.
Berislav, sometimes you simply can’t test for a method or object property.
Testing
typeof document.style.minHeightreturnsstringin all IE versions, but in IE 6 and lower, you can’t actually apply it, due to unsupported CSS propertymin-height.What’s even worse, some other
element.styleobject properties, that have unsupported CSS counterparts in IE 6 or lower (for exampledocument.style.maxHeight) return expectableundefined, so there’s no key one could rely on.Or maybe I’m missing something here…
Comment (#) by marko — 1st November 2006.
Nick, true that… thanks!
Comment (#) by marko — 1st November 2006.
You could use conditional compilation… untested but should work…
isIE7 = false;
/*@cc_on
@if (@_jscript_version == 7)
isIE7 = true;
@end
@*/
Comment (#) by DannyB — 15th November 2006.
Unfortunately, IE7 has an option to disable native support for XMLHTTP. Unchecking Tools > Options > Advanced > “Enable native XMLHTTP support” causes window.XMLHttpRequest to be undefined. Tested using following block:
<script>document.write(window.XMLHttpRequest);
</script>
Comment (#) by ken snyder — 18th November 2006.
Comment (#) by And Clover — 21st November 2006.
Sorry, the comment form is closed at this time, but if you have anything to say, please send me a message.