IE6 sniffer using conditional comments

I needed a fail-proof Internet Explorer 6 JavaScript sniffer and it seemed like everything I Googled warned of possible scenarios where it could possibly not work. Then It struck me that since I only needed to detect one version of Internet Explorer I could make a very simple script using Internet Explorer’s conditional comments.

I know what you are thinking, that JavaScript sniffers are outdated and go against all that is web standards. I agree. However, as IE6 fades into the past, isn’t it time we started ignoring instead of bending over backwards for its lack of standards support? This is where a JavaScript browser sniffer comes in!

Others have probably used this technique and expanded it to other versions of Internet Explorer as well. For this post, I am just going to discuss the IE6 version since it is very simple to expand it to all other versions of IE.

Drop this javascript snippet in a script tag in the head of your document:

var browserSniffer = {isIE6 : false}

Then, inside an IE conditional comment targeted at IE6, change the value of isIE6 to “true”. Like this:

browserSniffer = {isIE6 : true}

What you end up with is a non-global variable that is always equal to false unless the browser is IE6, then it is true. You can then easily dis-invite IE6 to the web standards party!

if (browserSniffer.isIE6 == true) {
	// fix some bug in IE6
}
else {
	// Do something sweet in other browsers
}

Standalone example – IE6 conditional comment javascript browser sniffer

Leave a Reply

Your name is required.
Your email will not be published.
A valid email address is required.
Optional!
You haven't added your comment!