HTML and CSS Reference
In-Depth Information
We'll use both XHTML and JavaScript comment types in our scripts. We'll use the
XHTML comment tag at the beginning of the JavaScript statement block to hide
JavaScript from older browsers. Some very old browsers will display the JavaScript code
rather than execute it. Encapsulating the JavaScript block in XHTML comment tags
hides the block from older browsers and the code is ignored by browsers that do not
support it.
To address all of the issues mentioned above, each JavaScript block would need the
following structure:
<script type="text/javascript">
<!-- <![CDATA[
... JavaScript code goes here
// ]]> -->
</script>
Let's take it apart to understand each line. Figure 14.7 shows the parts of this code.
Figure 14.7
Parts of the
JavaScript
statement block
structure
FAQ
If I'm using Transitional XHTML, can I simplify the JavaScript
code block?
Yes, you can omit the CDATA declaration information, but you should still include the XHTML
comments to hide the code from older browsers. The simpler block structure follows:
<script type="text/javascript">
<!--
... JavaScript code goes here
// -->
</script>
The JavaScript code is placed inside the statement block. This block can appear any-
where in the XHTML document and the code will be executed. Let's see how this
works, with a Hands-On example that will display an alert message.
 
Search WWH ::




Custom Search