Welcome to WebmasterWorld Guest from 54.159.179.132

Forum Moderators: open

Message Too Old, No Replies

XHTML, CDATA and embedded javascript

Avoiding XHTML javascript errors by using CDATA

   
8:04 pm on May 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Avoiding XHTML javascript errors by using CDATA: Using embedded javascript in XHTML documents requires special attention to avoid parsing errors. An earlier thread dealt with this problem. [webmasterworld.com...]

The WC3 provides instruction on how to wrap javascript functions to avoid misinterpretation by the XML processor. Review section: 4.8 Script and Style elements [w3.org...]

The solution is simple, it only requires the script be wrapped within a CDATA marked section.

<script>
<![CDATA[
... unescaped script content ...
]]>
</script>

Wrapping your script with CDATA is only needed for embedded (page) scripts, not for externally referenced instances. Heads up!

Thanks to madcat, Nick_W and tedster for bringing this to light.

7:01 am on May 27, 2002 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Thanks for the tip, papabaer ;)

Q: Using CDATA won't cause any problems in older browsers?

2:24 pm on May 27, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



That's what I thought Papabaer but then I found this:
Unfortunately, most browsers do not support CDATA sections (the standard XML method for including text containing special characters) so the safest option is to move your client-side scripts out to separate files and include them using the script element's src attribute.
- from O'Reilly - [xml.com...]

Granted this was published over a year ago but how do the browsers stack up now??

4:00 am on May 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Lorax-

I have a keen interest in knowing what other browsers recognize CDATA as well. For now I can provide only a few that do for sure, and maybe when people start using this tag in their sites a bit more they can add on...

IE5.5
IE6.0
OP6.01
OP6.03
NN6.2
NN7.0

M

1:45 pm on May 28, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Good idea Madcat, then at least we'll have a comprehensive list to look to should we need it.

How did you perform your tests? It might be good to have a standard benchmark for testing other browsers as well.

4:29 pm on May 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check out the section:

Q: I want the value of an element to be passed to the HTML result from an XSLT transformation as is (including special characters: >, <, &, etc.). How do I do that?

[xml.com ]

 

Featured Threads

Hot Threads This Week

Hot Threads This Month