views:

579

answers:

2

When researching JavaScript conditional comments for IE, I stumbled upon @cc_on. This seems to work. However, the wikipedia entry on conditional comments provides the following code for more robust IE detections, specifically IE6:

/*@cc_on
    @if (@_jscript_version > 5.7)
    document.write("You are using IE8+");

    @elif (@_jscript_version == 5.7 && window.XMLHttpRequest)
    document.write("You are using IE7");

    @elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest))
    document.write("You are using IE6");

    @elif (@_jscript_version == 5.5)
    document.write("You are using IE5.5");

    @else
    document.write("You are using IE5 or older");

@end

@*/

The issue is, I'm getting an "expected constant" javascript error on !window.XMLHttpRequest.

Clearly Wikipedia needs some help, and I need to get this working. Can anyone help me out?

+3  A: 

Definitely no JS expert, but some searches found this for isolating IE6 from IE7 using jscript_version == 5.7:

/*@cc_on
if (@_jscript_version==5.6 ||
   (@_jscript_version==5.7 &&
      navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1)) {
  //ie6 code
}
@*/

Maybe it'll point you in the right direction.

Source: http://sharovatov.wordpress.com/2009/06/03/efficient-ie-version-targeting/

JBickford
I'd like to clarify this a bit, but you're pretty much right. The issue turned out to be trying to call normal JavaScript within the precompiled conditional comment opperators "@elif" etc. When you just precompile the jscript version, and run everything else as traditional JavaScript, things compile and run just fine.
Jack
@Marcel Korpel - have you read my blogpost that JBickford is referring to? userAgent check is used *inside* conditional compilation block (so applied only to IEs) and there's no other way to determine a difference between IE7 which has jscript 5.7 and IE6 on Windows XP SP3 which also has jscript 5.7.
Vitaly Sharovatov
@Vitaly: I have to admit I didn't read your article and I was a bit harsh in my original comment, for which I apologize (and I deleted the comment). You have a point that you can safely detect the IE version in a conditional compilation block (and just that would be enough, also checking for JScript version is unnecessary), and browser detection *is* needed to check the `:hover` possibilities (they can't be detected, as far as I know), but I think using conditional comments is a more neat way to do this. That said, in most cases you could and should just use feature detection.
Marcel Korpel
@Marcel Just checking the navigator.userAgent string in a conditional compilation block is clearly not enough as IE6 can have both jscript of version 5.6 and 5.7 (when XPSP3 is installed). Therefore you have to combine both checks - for @_jscript_version AND navigator.userAgent if @_jscript_version returns 5.7.And there is no way to differentiate IE6 with jscript 5.6 or 5.7 from conditional comments - [if IE 6] will be parsed by both.Feature detection can do in most cases, but in some occasions with most obscure bugs you just need to know the exact version of jscript and trident.
Vitaly Sharovatov
@Vitaly: I agree with you that you need to look for the JScript version if you need specific features or want to tackle a bug involving JScript. The original question however was about detecting IE 6.
Marcel Korpel
A: 

I found a solution. The code is as follows.

<script type="text/javascript" charset="utf-8">
/*@cc_on
if (@_jscript_version > 5.7)
 document.write("You are using IE8");
else if (@_jscript_version == 5.7 && window.XMLHttpRequest)
 document.write("You are using IE7");
else if (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest))
 document.write("You are using IE6");
else if (@_jscript_version == 5.5)
 document.write("You are using IE5.5");
else
 document.write("You are using IE5 or older");
@*/
</script>
Yousui
Are you blind? That's the same chunk of code he included in his question.
Josh Stodola
Hi Josh, I changed @if...@elif...@else...@end to if...else if...else. I found some code in @if...@elif...@else...@end will not execute properly such as the **window.XMLHttpRequest** part. But it works well in normal if statements.
Yousui