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.
This only works in Netscape 4 or later, which limits its usefulness somewhat. MSIE supports an alternative, but completely incompatible technique that only works inside the <SCRIPT> tag.
<!--&{navigator.userAgent == "Mozilla/4.7 (Macintosh; I; PPC) "}; <SCRIPT> document.write("Power Macintosh running Navigator 4.7"); </SCRIPT> -->
See also: | Adding JavaScript to HTML |
Prev | Home | Next |
Conditional code block | Up | Conditional expression |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |