Hiding scripts from old browsers (Pitfall)

Old browsers need to have script content disguised.

If you are presenting your HTML on the web to an audience that may use browsers that cannot cope with the <SCRIPT> tag, then you will need to hide the script inside the <SCRIPT> block. This is done by placing HTML comment tags around the script code.

This depends on the JavaScript interpreter providing some non-standardized behavior. That is, it needs to recognize the HTML comment opening tag as being some valid syntax, even though it is not really legal.

Beware that the closing HTML comment tag is placed on a line that itself is a JavaScript comment. However, you must also make sure that the closing </SCRIPT> tag is placed on a line of its own or the interpreter cannot see it and the <SCRIPT> block then becomes unterminated.

Example code:

   <SCRIPT>

   <!-- 

   // Hide from older browsers

   alert("some script here");

   // end hiding -->

   </SCRIPT>

See also:COMMENT object, Pitfalls

Cross-references:

Wrox Instant JavaScript - page - 46