background
|
|
Games
Ruffnecks Gaming
Gaming for everyone
More icon

Tombstone
Tombstone Tuning
Home of tuning, projects and fast cars and boats.
More icon
|
|
Up Image
Navigation
Search this Site
Enter your search terms

Site Breadcrumb - You are here
|
The Comment Trick
Why the comment trick works:

Here's the trick again:

    <html>
    <head>
    <title> blah blah blah </title>

    <script language="JavaScript">

    <!-- hide this stuff from other browsers

    YOUR SCRIPT HERE

    // end the hiding comment -->

    </script> </head>
    <body>
    etc, etc, etc.

And here's why it works:

Normally, everything between <!-- and --> in an HTML page isn't displayed. This part of HTML works to hide JavaScript code from browsers that don't support JavaScript.

However, the clever people who invented JavaScript decided to make the <!-- tag mark the beginning of a one-line comment instead of the beginning of a multiline comment block (like it is in HTML). Because of this, the JavaScript interpreter will skip over that comment line (if it occurs between script tags) and happily continue processing the rest of your script.

Unfortunately, JavaScript doesn't know enough to ignore the closing comment tag (-->), so you have to comment it out by sticking the // at the beginning of the "end hiding comment" text. If you don't, JavaScript will try to interpret the --> as a JavaScript statement, and you'll get an error.

It's weird and tricky, but it works. I just do it by memory now. You can cut out the "hide this stuff" and "end hiding comment" text. All that matters is the <!-- on the line after the <script> tag and the // --> on the line before the </script> tag.

Remember, you wanted to know! So let's see ... where were we?