↑
Main Page
Changes in XHTML
Changes in XHTML
Recently, with the advent of the XHTML standard (eXtensible HTML), the
<script/>
tag has under-
gone a change. Instead of the
language
attribute, the tag is now expected to have a
type
attribute to
indicate the mime type of the inline code or external file being included; the mime type for JavaScript is
“text/javascript”
. For example:
<html>
<head>
<title>Title of Page</title>
<script type=”text/javascript”>
var i = 0;
</script>
<script type=”text/javascript” src=”../scripts/external.js”></script>
</head>
<body>
<!-- body goes here -->
</body>
</html>
Even though many browsers don’t fully support XHTML, most developers are now using the
type
attribute in place of the
language
attribute in anticipation of better XHTML support. Omitting the
language
attribute doesn’t cause any problems because, as noted earlier, all browsers default to
JavaScript for the
<script/>
tag.
The second change in XHTML is the use of CDATA sections. CDATA sections are used in XML (and,
therefore, in XHTML) to indicate text that should not be parsed as tags, allowing the use of special char-
acters such as the less-than (
<
), greater-than (
>
), ampersand (
&
), and double quotes (
“
) without using
their character entities. Consider the following code:
<script type=”text/javascript”>
function compare(a, b) {
if (a < b) {
alert(“A is less than B”);
} else if (a > b) {
alert(“A is greater than B”);
} else {
alert(“A is equal to B”);
}
}
</script>
This is a fairly simple function, which just compares two numbers,
a
and
b
, and then displays a message
indicating their relationship. In XHTML, however, this code is invalid because it uses three special char-
acters, less-than, greater-than, and double-quote. To fix this, you must replace these characters with their
XML entities,
<
,
>
, and
"
, respectively:
<script type=”text/javascript”>
function compare(a, b) {
if (a < b) {
alert("A is less than B");
} else if (a > b) {
131
JavaScript in the Browser
08_579088 ch05.qxd 3/28/05 11:37 AM Page 131
Free JavaScript Editor
Ajax Editor
©
→