A typical JavaScript handbook will have some instructions on writing hiding Javascript code from browsers that don't recognize it, but those instructions are basically worthless. The advice usually runs that you should write code that has the general structure
 1 <SCRIPT LANGUAGE="JavaScript">
 2 <!--
 3 JavaScript commands
 4 // -->
 5 </SCRIPT>
(The line numbers in red are not part of the code, but only placed for reference in this discussion.)

The idea is that within JavaScript, <!-- does not begin a multiline comment (like /* in C++ and Java) but only comments-out text following it until the end of the line (like // in C, C++, JavaScript itself and Java, and # in Perl or Unix scripts). Correspondingly, JavaScript does not recognize --> as an end-of-comment marker.

Thus, a JavaScript-enabled browser is unfazed by the one-line comment (line 2), interprets the commands (line 3), and ignores the ascii right-arrow at line 4 because it is commented-out with a double slash. (Lines 1 and 2 must not be interchanged, because then, the browser ignores the <SCRIPT> as part of an HTML; even in a JavaScript-enabled browser, JavaScript interpretation must be turned on before <!-- will be interpreted as a JavaScript (one-line) comment.)

To summarize the preceding paragraph, the insertion of lines 2 and 4 has no effect in a JavaScript-enabled browser. However, in earlier browsers, which do not know about JavaScript, things work out differently: the <SCRIPT> tag in line 1 (as well as the </SCRIPT> in line 5) is ignored, like all unknown tags, and lines 3-5 are simply an HTML comment. In this way, the JavaScript commands do not appear as uninterpreted text, which they would do without lines 2 and 4. One could even include alternative text intended only for non-JavaScript browsers, with lines like

<!-- --> Since you're using an ancient browser ...

In reality, however, you probably don't want to do this. One reason is that it's largely unnecessary: almost no one uses such an early browser -- downloading a newer free one is easy, and so many pages have Java or JavaScript that most surfers have acquired a recent version. The main graphical browsers (Netscape Navigator and Microsoft Internet Explorer) have included JavaScript interpretation since 1996.

The second reason is that in most of the few instances where you'd want it, it doesn't work. The most common instance is in browsers that are able to interpret JavaScript, but in which the user has disabled interpretation. These browsers still recognize the script even though they don't execute it. In particular, they understand that between <SCRIPT> </SCRIPT> tags, <!-- only indicates a single-line comment, and --> doesn't terminate it. So JavaScript commands won't be printed like HTML text, but your carefully inserted no-JavaScript message won't be relayed either. Less common are text-based browsers. These cannot execute JavaScript, but they do (nowadays) know about the <SCRIPT> tag, and do not print anything between it and the </SCRIPT> tag, so they effectively operate like JavaScript-capable browsers with interpretation disabled.

Return to SBF-glossary JavaScript entry.