In XHTML the parser tries to read any
< and & as the beginning of mark-up.
Using the &
and < for the & and < does NOT avoid this problem.
It may or may not be
possible to avoid this by escaping these characters (e.g.
/& and /<),
as Deitel does. (I am going to try to validate Deitel's code - stay
tuned.)
There are two
possible known solutions to this problem. (The problem is serious
when you start to
use JavaScript, as you need to have things like <h2>, <table>
etc inside your document.write(
) ‘s.)
The first
solution is the CDATA inserts that you see in all the programs I gave
you. CDATA
stands for Character Data and it tells the parser to copy the
following code
exactly (and not try to parse it.) This works in recent
browsers but some of
the older ones, reportedly, barf at this.
Note: I think
you could make the code look better by putting some more space
around the CDATA
stuff.
The second
solution is to put your scripts in separate files.
There are a few
things to note about this:
1. The actual XHTML page is very bare-bones - doesn't tell you anything
about what is on the
page.
2. The scripts in the external files are read right in to the page
so that
all the functions,
variables, etc. are available as though the scripts were in
your page.
For example, in the attached examples:
functions1wExternalJS.html
is the html page
f1_script_head.js
has the script that used to be in the head
(definitions of
functions)
f1_script_body.js
has the script which calls the functions in the
first script.
3. I have put the function
definitions in a separate JavaScript file as though they were a library of
functions, but if you don’t keep such a library you may put the function
definitions in the same file as the code, with the functions at the top.
4. Recall that the files which hold the scripts have js extensions and do
not need
<script> tags. The <script> tags which refer to them in the
main page
take care of that.
5. In this example, all 3 files
are in the same directory (or change the links to the js files appropriately)
and you ‘run’ the html page.