topics that matter; ideas worth sharing

share a tip, submit a link, add something new

HTML Comment Tags

August 6, 2002, 12:00 AM —  ITworld — 

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!

» posted by ITworld staff

ITworld

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources