HTML, XHTML, and CSS, Sixth Edition

Scripts sometimes contain symbols that have special meaning in XHTML documents, namely the & and the >. If you use these symbols in a script, you should enclose them in a CDATA section to hide them from XML parsers (the programs that ensure that the XML, or XHTML in this case, is properly written).

To hide internal scripts from XML parsers:

1.

Type <script> to begin your script as usual.

2.

Type <![CDATA[ to hide the script from the XML parser.

3.

Insert the script itself.

4.

Type ]]> to complete the CDATA section.

5.

And then type </script> to complete the script element.

Figure 19.19. This JavaScript script is adapted from Figure 20.1 on page 322. The CDATA section removes the special meaning from the characters in the script, essentially hiding them from the XML parser.

Tips

  • This is only necessary for XHTML documents with internal scripts. External scripts are automatically hidden from XML parsers.

  • It is also only necessary if you are serving your XHTML documents as XML. If you are serving them as HTML (which is true for the vast majority of Web pages), you don't have to enclose scripts in CDATA sections.

  • You can also hide internal style sheets in a CDATA section (see page 131).

Категории