Comments

How to add comments to user files

The ability to add comments to a file is very important. Comments can be used as part of the documentation, as an explanation, or just a note to say why and when a document was produced or edited. This page explains the syntax for adding comments to various types of files.

CSS

CSS comments start with /* and ends with */ This structure can include multiple comment lines.

/* This is a CSS comment */

/* This is a CSS comment
and so is this */

HTML

HTML comments can be a bit of problem, however a rule that should work in all browsers is that a comment starts with <!-- and ends with --> but must not contain -- or > anywhere in the comment. This structure can include multiple comment lines. There is a <comment></comment> tag but this is not cross-browser compliant and should not be used.

<!-- This is a HTML comment -->

<!-- This is a HTML comment
and so is this -->

JavaScript

JavaScript comments are a little odd. A pure JavaScript comment starts with /* and ends with */ This structure can include multiple comment lines.

/* This is a JavaScript comment */

/* This is a JavaScript comment
and so is this */

Single line comments can just be begun using //

// This is a JavaScript single line comment

Because JavaScript is used in conjunction with HTML, older browsers need to be told to ignore the JavaScript, otherwise they'll print it. In this case the following syntax needs to be used...

<SCRIPT LANGUAGE="JavaScript">

<!-- Use comment tags to hide the script from older browsers

// JavaScript comments can be used

JavaScript code can go here;

/* JavaScript multi-line comments
can be used as well */

// Stop hiding code from older browsers -->
</SCRIPT>

This can be more simply written as...

<SCRIPT LANGUAGE="JavaScript">
<--
JavaScript code goes here;
// -->
</SCRIPT>

The // before the --> is needed to stop JavaScript from trying to execute the -->

This page created 22nd November 2005, last modified 22nd November 2005


GoStats stats counter