Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Workshop: Hiding Scripts from Older Browsers

Since older browsers don't understand the <script> tag, they will not behave very well when they encounter a script in a Web page. In most cases, they will display the script in the middle of the page—probably not the effect you were looking for.

To avoid this, you can enclose the script within HTML comment tags. This tells older browsers to ignore the script completely. Newer browsers are smart enough to know that your script isn't really a comment.

HTML comments begin with the tag <!-- and end with the --> tag. Listing 2.4 shows a simple example of a script with comments.

Example 2.4. Hiding a script from older browsers

<SCRIPT LANGUAGE="JavaScript">
   <!--
   document.write("Your browser supports JavaScript.");
   // -->
</SCRIPT>

This script includes the beginning and ending HTML comment tags. The // in the last line is a JavaScript comment; this prevents the HTML comment from being detected as a JavaScript error.

Share ThisShare This

Informit Network