[thelist] (x)html 1.0 validation with javascript

Diane Soini dianesoini at earthlink.net
Thu Jun 24 21:05:04 CDT 2004

Where the CDATA section won't screw up the javascript because it's 

When you need it is, for example, if you had a for loop in some 
javascript on the page (not imported) like this:
The "<" would cause your XHTML to be invalid because the "<" character 
would be interpreted as the starting character of a tag.

Another example is if you have code concatenating some strings like 
such: mystr = '<a href="'+myvar+'">'+mytext+'</a>'.

The CDATA section tells the parser to consider the code inside as just 
text, analogous to (but obviously not the same as) <pre> tags in HTML, 
or heredoc syntax in some scripting languages that let you use 
characters that would otherwise cause problems.


On Thursday, June 24, 2004, at 01:29 PM, 
thelist-request at lists.evolt.org wrote:

> Subject: Re: [thelist] (x)html 1.0 validation with javascript
> Reply-To: "thelist at lists.evolt.org" <thelist at lists.evolt.org>
> Christopher Mahan wrote:
>> A reminder:
>> when putting a javascript in a xhtml 1.x doc, in the body, use the
>> cdata tag and escape it, like this:
>> <script type="text/javascript">
>>   // <[CDATA[
>>   function foo(){
>>   }
>>   // ]]>
>> </script>
>> Also note that you need the space after // and before <[CDATA[
>> as well as after // and before ]]>
>> Works in ie, moz, etc
> I have never seen this in use. Is it absolutely necessary? Are there 
> browsers where this will mess up the JavaScript?
Don't be afraid to try something new. An amateur built the ark. 
Professionals built the Titanic. -unknown

More information about the thelist mailing list