Conditional Compilations in Internet Explorer | JavaScript: Behavioral Layer

Standards Based Development

Conditional Compilation of JScript/JavaScript in ie

Conditional compilation is a feature in ie (ie4+) that acts as an absolute form of object detection, allow control over the browser in regards to which section(s) to compile, or not to compile, while depending on predefined/user defined conditions.

Just think conditional comments for JScript.

can be activated by calling @cc_on statement, or by using @if or @set statements which are part of the conditional compilation's logic. It's usually best to activate the @cc_on_ initially, because then you can include comment elements in the script to dance the browser compatibility dance.


<script type="text/javascript">
/*@cc_on
document.write("JScript version: " + @_jscript_version + ".<br>");
   /*@if (@_jscript_version >= 5)
      document.write("JScript Version 5.0 or better.<br \/>");
      document.write("This text is only seen by browsers that support JScript 5+<br>");
   @else @*/
      document.write("This text is seen by all other browsers (ie: Firefox, IE 4.x etc)<br>");
   /*@end
@*/
</script>
Conditional statements that can be utilized by conditional compilation:

if else Logic - ie JScript

The script below is only rendered by Trident, supplying different messages for operating systems that are or are not 32 bit:


if else Logic II - Other Browser Inclusive

The conditional compilation below manipulates a parsing error in the common tag purposefully, so that the else is rendered by non-ie browsers, as well as 32&:45bit versions of ie:


if, elseif, else Logic - ie JScript


/*@cc_on
   @if (@_jscript_version >= 5)
      document.write("IE Browser that supports JScript 5+");
   @elif (@_jscript_version >= 4)
      document.write("IE Browser that supports JScript 4+");
   @else
      document.write("Very old IE Browser");
   @end
@*/

if, elseif, else logic - Browsers Inclusive


/*@cc_on
   /*@if (@_jscript_version >= 5)
      document.write("IE Browser that supports JScript 5+");
   @elif (@_jscript_version >= 4)
      document.write("IE Browser that supports JScript 4+");
   @else @*/
      document.write("Non IE Browser (one that doesn't support JScript)");
   /*@end
@*/

As in the previous browser inclusive conditional compilation example, the last else statement above is also picked up by non-ie browsers.

Conditional Compilation of JScript/JavaScript in ie