By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

scriptalicious documentation suggests wrapping code in CDATA blocks - why?

P: n/a
I'm reading over this page:

http://wiki.script.aculo.us/scriptaculous/show/Usage

I'm struck by this code example
+++++++++++++++++++++++++++++++
3. Use

To call upon the functions, use HTML script tags. The best way is to
define them like this:

<script type="text/javascript" language="javascript">
// <![CDATA[
Effect.Appear('element_id');
// ]]>
</script>

+++++++++++++++++++++++++++++++
For god's sake, what are the CDATA blocks for? I've never seen that
before.

Jan 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Jake Barnes" <lk******@geocities.com> wrote:
I'm reading over this page:

http://wiki.script.aculo.us/scriptaculous/show/Usage

I'm struck by this code example
+++++++++++++++++++++++++++++++
3. Use

To call upon the functions, use HTML script tags. The best way is to
define them like this:

<script type="text/javascript" language="javascript">
// <![CDATA[
Effect.Appear('element_id');
// ]]>
</script>

+++++++++++++++++++++++++++++++
For god's sake, what are the CDATA blocks for? I've never seen that
before.


All HTML(ish things) in the CDATA should be ignored.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jan 7 '06 #2

P: n/a

Jake Barnes wrote:
I'm reading over this page:

http://wiki.script.aculo.us/scriptaculous/show/Usage

I'm struck by this code example
+++++++++++++++++++++++++++++++
3. Use

To call upon the functions, use HTML script tags. The best way is to
define them like this:

<script type="text/javascript" language="javascript">
// <![CDATA[
Effect.Appear('element_id');
// ]]>
</script>

+++++++++++++++++++++++++++++++
For god's sake, what are the CDATA blocks for? I've never seen that
before.


The CDATA tag, which also has a close tag, is an XML tag. When you
write a true xhtml page using the correct mime type
application/xhtml+xml, you often have to enclose everything after the
script tag up to the close script tag in CDATA tags. The CDATA tags
tell an xml parser that the contents within them are not part of the
xml(script in this case) and should not be parsed as xml. Some
characters often used in script code are forbidden in xml and, if the
parser detects them, the page may not be displayed and just gives you a
report of an xml error. You can avoid all of this by using the script
as an external script in the usual way. Unfortunately many current
browsers do not understand CDATA tags, and if you use them the script
does not work. This is solved by making the CDATA tags JS comments.
Besides the // in your example, the multi-line JS comments often are
used instead such as /* <![CDATA[ */ and /* ]]> */ . XML aware browsers
and servers, including the W3C validator see right through the JS
comment tags, find the CDATA tags, and are happy. The many browsers
that are not properly xml aware see the CDATA tags as just ordinary JS
comments, and they are happy and your script works.

Jan 7 '06 #3

P: n/a
On 07/01/2006 02:25, cw******@yahoo.com wrote:
Jake Barnes wrote:
[snip]
<script type="text/javascript" language="javascript">
The recommendation of the language attribute is a bad start.
// <![CDATA[


Suggesting CDATA Sections in what is conspicuously HTML is not a good
sign, either.

If you are serving HTML (including XHTML served as HTML, which you
shouldn't bother doing), then use:

<script type="text/javascript">
Effect.Appear('element_id');
</script>

If you are serving XHTML (with an application/xhtml+xml MIME type), then
use:

<script type="text/javascript">
<![CDATA[
Effect.Appear('element_id');
]]>
</script>

Don't mix them. See <http://www.hixie.ch/advocacy/xhtml>.

[snip]
The CDATA tag, which also has a close tag, is an XML tag.


CDATA Sections are a feature of SGML, and therefore HTML, too (see B.3.5
Marked Sections in the HTML Specification). Browsers don't recognise
them though as no common browser parses HTML as an application of SGML.
XML is an application profile of SGML, so therefore it features CDATA
Sections as well.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 7 '06 #4

P: n/a
Michael Winter wrote:
On 07/01/2006 02:25, cw******@yahoo.com wrote:
The CDATA tag, which also has a close tag, is an XML tag.
CDATA Sections are a feature of SGML, and therefore HTML, too (see B.3.5
Marked Sections in the HTML Specification).


In the HTML 4.01 Specification, to be precise:

<URL:http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.5>
Browsers don't recognise them though as no common browser parses HTML as
an application of SGML. XML is an application profile of SGML, so
therefore it features CDATA Sections as well.


Thank you for pointing this out. I thought that marked sections were only
defined by XML (and for XML applications) before.
Regards,
PointedEars
Jan 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.