If you are reading this, please get modern browser.
skip to main content | skip to main navigation | skip to secondary content

Reliable IE 7 detection with JavaScript?

~ 31st October 2006. · 19:15 CET · permanent link · printer friendly ~

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 true in all Internet Explorer versions and also in Opera
window.opera
also known as opera object, returns true in Opera
window.XMLHttpRequest
returns true in Firefox, Opera, Safari and IE 7
returns false in 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

  1. 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.)

  2. Wow, great tip about window.XMLHttpRequest, which I might find a use for, very soon.

  3. 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.

  4. Berislav, sometimes you simply can’t test for a method or object property.

    Testing typeof document.style.minHeight returns string in all IE versions, but in IE 6 and lower, you can’t actually apply it, due to unsupported CSS property min-height.

    What’s even worse, some other element.style object properties, that have unsupported CSS counterparts in IE 6 or lower (for example document.style.maxHeight) return expectable undefined, so there’s no key one could rely on.

    Or maybe I’m missing something here…

  5. Nick, true that… thanks!

  6. You could use conditional compilation… untested but should work…
    isIE7 = false;
    /*@cc_on
      @if (@_jscript_version == 7)
        isIE7 = true;
      @end
    @*/

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

  8. @if (@_jscript_version == 7)

    Incidentally the correct _jscript_version value for IE7 is 5.7. (Similarly, IE6 is 5.6.)

Sorry, the comment form is closed at this time, but if you have anything to say, please send me a message.

* Please keep in mind that this is a personal web site and it does not reflect the position or opinion of my respective employers, organizations or partners.

Typetester – compare screen type Supported by Veer.

What is this?

A web log of Marko Dugonjić, web professional from Croatia. Topics covered:

Translate this site

German, Spanish, Italian, French or Japanese (via).

See you there!

Feel like buying a book?

Try with maratz.com aStore

Worth visiting

top of the page | skip to main content | skip to main navigation | skip to secondary content