Re: SCRIPT and embedded markup

Undeference@netscape.net wrote:
> Obviously code like below is a nesting problem:
> <script type="text/javascript">
> // This should go between <head> and </head>
> </script>
> 
> But the reason for there being even a mention to the invalidity of the
> following is astounding (from
> <http://www.htmlhelp.com/tools/validator/problems.html#script>:
> 
> <script type="text/javascript">
> <!--
>   // This is an error!
>   document.write("</P>");
> // -->
> </script>
> 
> The purpose of commenting out the script content is to hide it from
> old (really old) browsers so that only newer browsers handle it.

Yes, but the only reason it works is because no browser reads the DTD; 
and such browsers are, therefore, unawre that the script element is 
defined with a content model of CDATA (character data) and continue to 
process it as PCDATA (parsed character data).

> However, just because UAs stoop to the level of snooping out comments
> to support an outdated and improper hack does not mean a validator
> should.

The reason browsers read the content of the script, despite it appearing 
to be within an HTML comment <!-- ... --> is because it's not really a 
comment.  Please see HTML Comments in Scripts for a thourough explanation.

http://lachy.id.au/log/2005/05/script-comments

> As far as a validator that *properly* parses HTML or XML documents,
> the above code should be exactly identical to the following:
> <script type="text/javascript">
> 
> </script>

That's correct for XHTML, but wrong for HTML.

-- 
Lachlan Hunt
http://lachy.id.au/

Received on Wednesday, 1 June 2005 13:38:04 UTC