Detecting IE7+ in javascript

via ajaxian:

Abe Fettig knows that we need to start detecting the difference between IE6- and IE7+, because a lot of the hacks that we were using for IE are no longer needed.

He didn’t want to grok the user agent, as that is very brittle, so he came up with:

if (typeof document.body.style.maxHeight != “undefined”) {
// IE 7, mozilla, safari, opera 9
} else {
// IE6, older browsers
}

Any other thoughts?


Indeed there are many thoughts on the subject; the post illicited over 20 comments with other solutions:

You can also use the XHR check:

if (window.XMLHttpRequest) {
   // IE 7, mozilla, safari, opera 9
} else {
   // IE6, older browsers
}

Yeah, how about using Conditional Compilation of JavaScript in IE

Conditional comments could also be used. eg.


isIE7 = true;

.. Or something along those lines. See @MSDN.

> More

Leave a Reply

You must be logged in to post a comment.