Re: Java script and validator... not the common endtag/backslash error

On Sat, 21 Aug 2004, Jari Porola wrote:

> I have javascript which has writeln() statements and
> validator parses the content of the script.

As usual, telling the URL would save time and guesses.

But presumably you are using XHTML, where <script> elements have #PCDATA
as their content model, as opposite to CDATA in HTML. This means that
in XHTML parsing, the content is "parsed", in the XML sense, i.e. looking
for markup, including comment declarations.

> invalid comment declaration: found delimiter "'"
> outside comment but inside comment declaration
> d.writeln( '<!--' );

I wonder what particular situation causes exactly that. But in any case,
in #PCDATA the character pair <! starts a markup declaration, and then
you're in trouble. (In SGML or XML parsing, no attention is paid to
quotation marks or apostrophes in #PCDATA - they are just character data,
without starting a quoted string of any kind.)

> I know I can skip these by putting the script in
> separate file,

Yes, that's the best approach.

> but I can't do that since my PHP
> gerates the javascript. so I have PHP which generates
> javascript which generates javascript.

It's hard to guess what the situation really is. But anyway, if the use of
an external script file is excluded, then there are various options.
The theoretical option is a CDATA section. The practical way (if you care
about IE, for example, which is XHTML ignorant and doesn't grok CDATA
sections either) is to use workarounds that avoid the constructs that
trigger something in #PCDATA parsing. For example, generating the "<"
character in JavaScript in a manner that does not mention it literally
removes the particular problem mentioned above:

var lt = '\x3c';
d.writeln( lt + '!--' );

(If I didn't explain this clearly enough, you might check the explanation
in Finnish in Korpela & Linjama, "XHTML-käsikirja", Docendo, August 2004,
pages 727 - 728.)

Jukka "Yucca" Korpela,

Received on Saturday, 21 August 2004 18:04:18 UTC