This means that 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. by using /& and /<),
as Deitel does. (I am going to try to validate Deitel's code - stay tuned.)
The problem is serious because when you start to
use JavaScript, you still need to have things like <h2>, <table>
etc. inside your document.write( ) 's.
There are two possible known solutions to this problem.
The first solution is the CDATA surrounds 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.
Recall that the sections begin with //<![CDATA[
and end with //]]>
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 - it doesn't tell you anything
about what is happening on the page.
2. The scripts in the external files are read right into 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 three files
are in the same directory (or change the links to the js files appropriately)
and you load the html page.