HTML and CSS Reference
In-Depth Information
JavaScript and XML Parsers
Embedding JavaScript code within an XHTML file can lead to problems because XHTML
parsers attempt to process the symbols in JavaScript code. Character symbols such as
angle brackets (< >) and the ampersand (&) are often used in JavaScript programs and
can lead to a page being rejected by an XHTML parser. To avoid this problem, you can
place your JavaScript code within a CDATA section as follows:
<scriptƒtype=”text/javascript”>
<![CDATA[
ƒƒƒ JavaScriptƒcode
]]>
</script>
where JavaScriptƒcode is the code that makes up the JavaScript program. The
CDATA section marks the text of the JavaScript code as data that should not be pro-
cessed by XHTML parsers. Unfortunately, CDATA sections are not well supported by
current browsers.
Another alternative is not to embed your scripts within XHTML files at all, but instead
to place them in external files. This practice has the added advantage of separating
program code from page content. If you need to create valid XHTML documents, this is
probably the best solution.
Writing Output to a Web Document
The first JavaScript program you'll add to Kate's document is a program that writes the text of
an e-mail address into the Web page. Although you could enter the e-mail address directly,
you'll use this opportunity to experiment with JavaScript. You'll also build on this simple
statement as you progress through the rest of the tutorial. You'll insert the e-mail address for
Catherine Adler as the first entry in the staff directory. Her e-mail address is cadler@mpl.gov .
To write this text to the Web document, you'll insert the following statement:
<scriptƒtype=”text/javascript”>
ƒƒƒdocument.write(“cadler@mpl.gov”);
</script>
The document.write() statement tells browsers to send the text string cadler@mpl . gov
to the Web page document. To see how your browser applies this command, you'll enter
the script element and command into Kate's mpl.htm file now.
To write text to the Web page using JavaScript:
1. Return to the mpl.htm file in your text editor.
2. Locate the table cell after the entry for Catherine Adler and insert the following
code, as shown in Figure 10-8:
<scriptƒtype=”text/javascript”>
ƒƒƒdocument.write(“cadler@mpl.gov”);
</script>
Search WWH ::




Custom Search