HTML and CSS Reference
In-Depth Information
16.3.7. Handling Special Characters
XHTML is more sensitive than HTML to the use of the < and & characters
in JavaScript and CSS declarations within your documents. In HTML, you
can avoid potential conflicts by enclosing your scripts and stylesheets
in comments ( <!-- and --> ). XML browsers, however, may simply re-
move all the contents of comments from your document, thereby delet-
ing your hidden scripts and stylesheets.
To properly shield your special characters from XML browsers, enclose
your styles or scripts in a CDATA section. This tells the XML browser that
any characters contained within are plain old characters, without special
meanings. For example:
<script language="JavaScript">
...JavaScript here...
This doesn't solve the problem, though. HTML browsers ignore the con-
tents of the CDATA XML tag but honor the contents of comment-enclosed
scripts and stylesheets, whereas XML browsers do just the opposite. We
recommend that you put your scripts and styles in external files and ref-
erence them in your document with appropriate external links.
Special characters in attribute values are problematic in XHTML, too. In
particular, you always should write an ampersand within an attribute
value using &amp; and not simply an & character. Similarly, play it safe
and encode less-than and greater-than signs using their &lt; and &gt;
entities. For example, while:
<img src=seasonings.gif alt="Salt & pepper">
is perfectly valid HTML, you must write it as:
Search WWH ::

Custom Search