Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/*@cc_on and IE6 detection

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?

like image 406
Jack Avatar asked Dec 03 '09 22:12

Jack


3 Answers

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/

like image 119
JBickford Avatar answered Sep 22 '22 13:09

JBickford


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>
like image 33
Just a learner Avatar answered Sep 24 '22 13:09

Just a learner


I've been using this nice one-liner for years:

var IE; //@cc_on IE = parseFloat((/MSIE[\s]*([\d\.]+)/).exec(navigator.appVersion)[1]);

Small and accurate (tested in IE 6-10).

Note to those who use grunt. be sure to set preserveComments: 'some' if you use the uglify plugin to make sure conditional comments are not removed.

like image 36
will Farrell Avatar answered Sep 21 '22 13:09

will Farrell