Sometimes you just have to sniff for Internet Explorer 6 (and under) in JavaScript. Using conditional comments is a decent solution, but I don't want them scattered all over my code.
With a bit of help from Dean Edwards, I worked out the following:
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
With just a single conditional comment, you can have a JavaScript variable that you can reuse throughout your code.
(IE6 will be true in Internet Explorer 6 and under, but does anyone really care about IE5 anymore? Thought not.)
You could use this technique to sniff for other things:
// exactly Internet Explorer 7 var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/; // at least Internet Explorer 7 var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/; // any Internet Explorer (thanks to Dean) var isMSIE = /*@cc_on!@*/false;
Note: browser sniffing is evil but sometimes painfully necessary.
Trouble is, MS recently released JScript 5.7 as a download for IE6 users, so this method of detection promptly fails. Annoyingly.
That conditionals are a good solutions, recently I'm being using this snipped:
function vIE(){return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;}
to get the right version of IE (or -1 for the rest of browsers)