Re: document.write and validation

by "tim booker" <timbooker(at)btinternet.com>

 Date:  Mon, 12 Mar 2001 15:11:24 -0000
 To:  "DAKennedy" <taxform(at)ijntb.net>,
"Hwg-Techniques" <hwg-techniques(at)hwg.org>
 References: 
  todo: View Thread, Original
Hello.

That WAS a "spur of the moment example with a typo", but I think the second
part of your message has answered my question.

Thank you,

Tim

www.timbooker.com




----- Original Message -----
From: "DAKennedy" <taxform(at)ijntb.net>
To: "tim booker" <timbooker(at)btinternet.com>
Sent: Monday, March 12, 2001 3:00 PM
Subject: Re: document.write and validation


On or about 11:49 AM 03/12/2001 -0000, tim booker alleged:


>When I try to validate a page that uses document.write to insert HTML, the
>W3C validator gives me an error.
>
>For example, if I had:
>
>document.write('<a href="'index.html">click here</a>');
>
 If that's an actual piece of code, rather than just a spur of the moment
example with a typo, you might want to remove the extra apostrophe/single
quote mark in the HREF attribute value.

>The validator doesn't seem to see the opening tag, and so reports an error
>when it sees the closing.
>
 Correct.  From the "validator's" point of view it sees only an unpaired
closing tag.

>Is that section of JavaScript invalid?  Or, is it a problem with the
>validator that causes it not to see the opening tag?
>
 Neither and both.  :o)
 The validator correctly sees the opening tag as a string literal, not as
an actionable HTML tag.  The validator incorrectly sees the closing tag as
an actionable HTML element, not as a string literal.
 The difference is the "/" (forward slash) in the closing tag.  The
validator sees that the "/" is unescaped and assumes it to be actionable
(rather than display only) and so looks for the opening tag of the element,
which it soesn't find, resulting in the error.

>How can I get around this validation problem?
>
 By "escaping" the problem character so it is read correctly as a string
literal to be displayed "as is."  The escape character is a "\" (back
slash) and it is placed immediately before the "actionable" character.
 In your example change </a> to <\/a> and it will validate without
affecting its operation or function.

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA