Re: HTML Comments, JavaScript and Validator

From: Chris Nappin (CNappin@inri.co.uk)
Date: Thu, Jun 17 1999


Message-ID: <3768AAAD.3F15587C@inri.co.uk>
Date: Thu, 17 Jun 1999 08:58:37 +0100
From: Chris Nappin <CNappin@inri.co.uk>
To: James Bromberger <james@publishing.uwa.edu.au>
CC: "validator (email list)" <www-validator@w3.org>
Subject: Re: HTML Comments, JavaScript and Validator

James Bromberger wrote:

> Is it just me, or doed the Validator (validator.w3.org) seem to try to
> validate JavaScript despite the JavaScript being within HTML comments?
>
> Ie, with:
>         <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
>         <!--
>                 document.write("<P>" + desc + "</P>")
>         // -->
>         </SCRIPT>
>
> The validator highlights the closing tag and says:
>         "end tag for element "P" which is not open "
>
> This is obviously not the case - the P element is opened and closed
> immediately in the write method.
>
> Regardless, I don't want HTML validation on JavaScript! Surely getting
> the validator to ignore everything up to the closing comment delimeter
> would be correct.

Sorry if you've recieved many similar replies...

All data between the opening and closing script tags must correspond to the CDATA format. So it does get validated. To quote the HTML 4 spec (section 6.2):

  The first occurrence of the character sequence "</" (end-tag open delimiter) is treated as
     terminating the end of the element's content.

So technically, the </p> in the document.write string is ending your script and causing your errors. The solution is to escape the string to "<\/p>".

This question gets raised many, many times by those new to the validator, and TIDY. Please add it to a FAQ !

thanks,

--
Chris Nappin, INRI UK Ltd.
Tel: +44 (0)1703 760484 Fax: +44 (0)1703 760483 Web: www.inri.co.uk
INRI is a subsidiary of Logicon Inc, a Northrop Grumman Company.