views:

11578

answers:

14

Are CDATA tags ever necessary in script tags and if so when?

In other words, when and where is this:

<script type="text/javascript">
//<![CDATA[
...code...
//]]>
</script>

preferable to this:

<script type="text/javascript">
...code...
</script>
A: 

When you want it to validate (in XML/XHTML - thanks, Loren Segal).

ceejayoz
+2  A: 

When you are going for strict XHTML compliance, you need the CDATA so less than and ampersands are not flagged as invalid characters.

Chris Shaffer
+23  A: 

A CDATA section is required if you are using XHTML in order for it to validate, as XHTML will parse the JavaScript code as parsed character data as opposed to character data by default. This is not an issue with scripts that are stored in external source files, but for any inline JavaScript in XHTML you will want to use a CDATA section.

For a good writeup on the subject, see http://javascript.about.com/library/blxhtml.htm

Michael Ridley
There's a lot more to it than just "validation". Most strict XML parsers won't go through the page if they hit an illegal character. It's more than simply about making W3C happy and getting green instead of red.
Loren Segal
If you avoid `
bobince
What about in the case of HTML5?
Mathew Attlee
+1  A: 

Do not use CDATA in HTML4 but you should use CDATA in XHTML and must use CDATA in XML if you have unescaped symbols like < and >.

Loren Segal
Why not in HTML4?
Robert Claypool
CDATA is not valid in HTML4. Simply put, it's not part of the grammar. CDATA is a syntax of XML, and XHTML is an XML subset. Therefore it should only be used inside XML (and its subsets). HTML on the other hand is not XML.
Loren Segal
+1  A: 

When browsers treat the markup as XML:

<script>
<![CDATA[
    ...code...
]]>
</script>

When browsers treat the markup as HTML:

<script>
    ...code...
</script>

When browsers treat the markup as HTML and you want your XHTML 1.0 markup (for example) to validate.

<script>
//<![CDATA[
    ...code...
//]]>
</script>
Shadow2531
+11  A: 

HTML

An HTML parser will treat everything between <script> and </script> as part of the script. Some implementations don't even need a correct closing tag; they stop script interpretation at "</". [edit] ...which is correct according to the specs [/edit].

So, in HTML, this is not possible:

<script>
var x = '</script>';
alert(x)
</script>

A CDATA section has no effect at all. That's why you need to write

var x = '<' + '/script>'; // or
var x = '<\/script>';

or similar.

This also applies to XHTML files served as text/html. (Since IE does not support XML content types, this is mostly true.)

XML

In XML, different rules apply. Note that (non IE) browsers only use an XML parser if the XHMTL document is served with an XML content type.

To the XML parser, a script tag is no better than any other tag. Particularly, a script node may contain non-text child nodes, triggered by "<"; and a "&" sign denotes a character entity.

So, in XHTML, this is not possible:

<script>
if (a<b && c<d) {
    alert('Hooray');
}
</script>

To work around this, you can wrap the whole script in a CDATA section. This tells the parser: 'In this section, don't treat "<" and "&" as control characters.' To prevent the Javascript engine from interpreting the "<![CDATA[" and "]]>" marks, you can wrap them in comments.

If your script does not contain any "<" or "&", you don't need a CDATA section anyway.

Pumbaa80
+3  A: 

to avoid xml errors during xhtml validation.

marduk
A: 

That way older browser don't parse the Javascript code and the page doesn't break.

Backwards compatability. Gotta love it.

Chacha102
+1  A: 

CDATA tells the browser to display the text as is and not to render it as an HTML.

Ikaso
+8  A: 

It's an X(HT)ML thing. When you use symbols like < and > within the JavaScript, e.g. for comparing two integers, this would have to be parsed like XML, thus they would mark as a beginning or end of a tag.

The CDATA means that the following lines (everything up unto the ]]> is not XML and thus should not be parsed that way.

Franz
+3  A: 

CDATA indicates that the contents within are not XML.

http://en.wikipedia.org/wiki/CDATA

Alex Beardsley
+3  A: 

It to ensure that XHTML validation works correctly when you have javascript embdeded in your page, rather than externally referenced.

XHTML requires that your page strictly conform to XML markup requirements. Since javascript may contain characters with special meaning, you must wrap it in CDATA to ensure that validation does not flag it as malformed.

With HTML pages on the web you can just include the required Javascript between and tags. When you validate the HTML on your web page the Javascript content is considered to be CDATA (character data) that is therefore ignored by the validator. The same is not true if you follow the more recent XHTML standards in setting up your web page. With XHTML the code between the script tags is considered to be PCDATA (parsed character data) which is therefore processed by the validator.

Because of this, you can't just include Javascript between the script tags on your page without 'breaking' your web page (at least as far as the validator is concerned).

You can learn more about CDATA here, and more about XHTML here.

LBushkin
+3  A: 

Basically it is to allow to write a document that is both XHTML and HTML. The problem is that within XHTML, the XML parser will interpret the &,<,> characters in the script tag and cause XML parsing error. So, you can write your javascript with entities, e.g.:

if (a &gt; b) then alert('hello world');

But this is impractical. The bigger problem is that if you read the page in HTML, the tag script is considered CDATA 'by default', and such javascript will not run. Therefore, if you want the same page to be OK both using XHTML and HTML parsers, you need to enclose the script tag in CDATA element in XHTML, but NOT to enclose it in HTML.

This trick marks the start of CDATA element as javascript comment; in HTML the javascript parser ignores the CDATA tag (it's a comment). In XHTML, the XML parser (which is run before the javascript) detects it and treats the rest until end of CDATA as CDATA.

ondra
A: 
Craig