From: www.itworld.com

HTML Comment Tags

by Paul V. Miller

August 6, 2002 —

 

Greetings HTML writers of the world. Welcome back to another week of the
HTML newsletter. Thanks for tuning in.

Not much thought goes into the use of the these little buggers most of
the time. Most HTML coders learn this kind of tag early on and promptly
forget about it. Occasionally you might throw one into a page to, well,
make a comment, but this little tag actually has plenty of other uses as
well.

Let's begin by considering the comment tags function. In the simplest of
terms, anything within comment tags in your code is invisible to your
browser. The exception, of course, is when a comment tag is used in
tandem with the script or style tag.

Under most circumstances though, a normal comment with a little text in
it looks like this:

Hey there cats and kittens!

Anything within the comment tags is ignored so that only a person
viewing the source code can see it, which is exactly who the comment is
directed to -- anyone looking at the source code. Comment tags can also
be used to address other Web designers:

Commenting Out Code
What else can we use this tag for? How about as a way to erase code from
your page while keeping it in the HTML document and in it's appropriate
place? Let's say you want to erase a hyperlinked site while the site
happens to be down for maintenance. When it goes back up, you want to
restore the link:

Yahoo!
Hotmail!
Toast!
Beer!

If hotmail goes down, then simply insert the comment tags around the
code:

Yahoo!

Toast!
Beer!

Your code stays right where it is so if you need to access it at any
time, you can. I use this trick a lot when I'm designing sites that use
a lot of divs (http://www.itworld.com/nl/html_tutor/11202001). It's
almost like temporarily turning the div content off.

Comments are also a good way to isolate problems on your pages. If you
have a lot of code and you can't find the part causing the problem, then
try commenting out different chunks until you find the offending code.

Comment Tags Within Script and Style Tags
As I mentioned earlier, there's a slight exception to the normal rules
when a comment tag is used inside a script or style tag. If you've seen
JavaScript code before, comment tags are usually used to help guard
against older browsers accidentally seeing unrecognized script and
printing it out on a page. Here's an example:

When most browsers see this, they recognize the script tag and
understand that everything contained within it is going to be
JavaScript. The browser then goes about following the JavaScript's
instructions. Since the opening HTML comment tag is ignored in
JavaScript, no harm is done. However, JavaScript does not know what the
closing HTML comment tag is so a JavaScript comment tag -- // -- is
added before it. The JavaScript comment tag effects only the characters
that appear on the same line after it, meaning JavaScript ignores the
closing HTML comment tag as instructed.

Older browsers, which don't recognize the script tag, would ignore it
all together. That's okay though, because the next thing they'd see
would be the comment tags. Everything inside (the JavaScript itself) is
ignored, as is the closing script tag (since it isn't recognized). No
harm, no foul.

The same technique is used for the style tag. Check out our tutorials on
CSS in the archives section of the ITworld Web site (Introduction to
Cascading Style Sheets : http://www.itworld.com/nl/html_tutor/06262001).

That's it for this week. Experiment with those comment tags and join us
next week for more HTML fun!