Main Page

Changes in XHTML

Changes in XHTML
Recently, with the advent of the XHTML standard (eXtensible HTML), the
tag has under-
gone a change. Instead of the
attribute, the tag is now expected to have a
attribute to
indicate the mime type of the inline code or external file being included; the mime type for JavaScript is
. For example:
<title>Title of Page</title>
<script type=”text/javascript”>
var i = 0;
<script type=”text/javascript” src=”../scripts/external.js”></script>
<!-- body goes here -->
Even though many browsers don’t fully support XHTML, most developers are now using the
attribute in place of the
attribute in anticipation of better XHTML support. Omitting the
attribute doesn’t cause any problems because, as noted earlier, all browsers default to
JavaScript for the
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”);
This is a fairly simple function, which just compares two numbers,
, 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 &lt; b) {
alert(&quot;A is less than B&quot;);
} else if (a &gt; b) {
JavaScript in the Browser
08_579088 ch05.qxd 3/28/05 11:37 AM Page 131

JavaScript EditorFree JavaScript Editor     Ajax Editor