Zenscope Studio

Microsoft Brings Conditional Comments to JavaScript

For better or worse, Microsoft’s concept of conditional comments is also available for the Web’s favorite scripting language. The company calls this feature “conditional compilation.” It could be used to address cross-browser JavaScript incompatibilities by using features unique to Microsoft’s “JScript” implementation of the language.

Here’s the example that Microsoft offers in their own documentation:

/*@cc_on @*/
/*@if (@_jscript_version >= 4)
  alert("JScript version 4 or better");
  @else @*/
  alert("You need a more recent script engine.");
/*@end @*/

Although this feature is documented on Microsoft’s site as part of the .Net runtime, it’s actually been around since IE 4. According to Brian Lalonde’s guide to JavaScript browser detection, it’s even available in IE for Macintosh. Along with JScript version checking, there are several conditions to determine on which platform the script is running.

For a more thorough examination of conditional compilation and its uses, check out this tutorial from JavaScript Kit.

(Props: Dean Edward by way of Aaron Newton)

Comments

Comments are closed for this article.