Conditional comment (HTML Tag)

A portability trick that only works in Netscape.


JavaScript - 1.2
Netscape - 4.0

Conditional comments use JavaScript entities to enclose a block of JavaScript and only execute it conditionally on some value being true.

It is accomplished by embedding a JavaScript entity with a logical expression evaluation in it. If the expression proves true then the <SCRIPT> HTML tag enclosed in the comment block is parsed, otherwise it is ignored.

This is how it's done. A conditional comment is formed by adding an ampersand character to the leading tag of a comment. Rather than use "<!--" the comment is introduced with "<!--&" instead. The comment is closed in the normal way with a trailing "-->" string. Inside the comment a <SCRIPT></SCRIPT> block is placed with some global code to be executed if called for.


Example code:

   <!--&{navigator.userAgent == "Mozilla/4.7 (Macintosh; I; PPC) "};
   document.write("Power Macintosh running Navigator 4.7");

See also:Adding JavaScript to HTML