<p>Are <strong>CDATA</strong> tags ever necessary in script tags and if so when?</p>

<p>In other words, when and where is this:</p>

<pre><code>&lt;script type="text/javascript"&gt;
//&lt;![CDATA[
...code...
//]]&gt;
&lt;/script&gt;
</code></pre>

<p>preferable to this:</p>

<pre><code>&lt;script type="text/javascript"&gt;
...code...
&lt;/script&gt;
</code></pre>

[{'answer_id': 66848, 'author': 'ceejayoz', 'author_id': 1902010, 'author_profile': 'https://Stackoverflow.com/users/1902010', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://javascript.about.com/library/blxhtml.htm" rel="nofollow noreferrer">When you want it to validate</a> (in XML/XHTML - thanks, <a href="https://stackoverflow.com/users/6436/loren-segal">Loren Segal</a>).</p>\n'}, {'answer_id': 66864, 'author': 'Chris Shaffer', 'author_id': 6744, 'author_profile': 'https://Stackoverflow.com/users/6744', 'pm_score': 3, 'selected': False, 'text': '<p>When you are going for strict XHTML compliance, you need the CDATA so less than and ampersands are not flagged as invalid characters.</p>\n'}, {'answer_id': 66865, 'author': 'Michael Ridley', 'author_id': 4838, 'author_profile': 'https://Stackoverflow.com/users/4838', 'pm_score': 10, 'selected': True, 'text': '<p>A CDATA section is required if you need your document to parse as XML (e.g. when an XHTML page is interpreted as XML) <em>and you want to be able to write literal <code>i&lt;10</code> and <code>a &amp;&amp; b</code> instead of <code>i&amp;lt;10</code> and <code>a &amp;amp;&amp;amp; b</code></em>, 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 <em>probably</em> want to use a CDATA section.</p>\n\n<p>Note that many XHTML pages were never intended to be parsed as XML in which case this will not be an issue.</p>\n\n<p>For a good writeup on the subject, see <a href="https://web.archive.org/web/20140304083226/http://javascript.about.com/library/blxhtml.htm" rel="noreferrer">https://web.archive.org/web/20140304083226/http://javascript.about.com/library/blxhtml.htm</a></p>\n'}, {'answer_id': 66878, 'author': 'Loren Segal', 'author_id': 6436, 'author_profile': 'https://Stackoverflow.com/users/6436', 'pm_score': 4, 'selected': False, 'text': '<p>Do <em>not</em> use CDATA in HTML4 but you <em>should</em> use CDATA in XHTML and <em>must</em> use CDATA in XML if you have unescaped symbols like &lt; and >.</p>\n'}, {'answer_id': 66900, 'author': 'Shadow2531', 'author_id': 1697, 'author_profile': 'https://Stackoverflow.com/users/1697', 'pm_score': 8, 'selected': False, 'text': '<p>When browsers treat the markup as XML:</p>\n\n<pre><code>&lt;script&gt;\n&lt;![CDATA[\n    ...code...\n]]&gt;\n&lt;/script&gt;\n</code></pre>\n\n<p>When browsers treat the markup as HTML:</p>\n\n<pre><code>&lt;script&gt;\n    ...code...\n&lt;/script&gt;\n</code></pre>\n\n<p>When browsers treat the markup as HTML and you want your XHTML 1.0 markup (for example) to validate.</p>\n\n<pre><code>&lt;script&gt;\n//&lt;![CDATA[\n    ...code...\n//]]&gt;\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 1450633, 'author': 'user123444555621', 'author_id': 27862, 'author_profile': 'https://Stackoverflow.com/users/27862', 'pm_score': 7, 'selected': False, 'text': '<h2>HTML</h2>\n\n<p>An HTML parser will treat everything between <code>&lt;script&gt;</code> and <code>&lt;/script&gt;</code> as part of the script. <strike>Some implementations don\'t even need a correct closing tag; they stop script interpretation at "<code>&lt;/</code>", which is correct according to the <a href="http://www.w3.org/TR/html401/appendix/notes.html#notes-specifying-data" rel="noreferrer">specs</a></strike>. </p>\n\n<blockquote>\n  <p><strong>Update</strong> In HTML5, and with current browsers, that is not the case anymore.</p>\n</blockquote>\n\n<p>So, in HTML, this is <em>not</em> possible:</p>\n\n<pre><code>&lt;script&gt;\nvar x = \'&lt;/script&gt;\';\nalert(x)\n&lt;/script&gt;\n</code></pre>\n\n<p>A <code>CDATA</code> section has <strong>no effect at all</strong>. That\'s why you need to write</p>\n\n<pre><code>var x = \'&lt;\' + \'/script&gt;\'; // or\nvar x = \'&lt;\\/script&gt;\';\n</code></pre>\n\n<p>or similar.</p>\n\n<p>This also applies to XHTML files served as <code>text/html</code>. (Since IE does not support XML content types, this is mostly true.)</p>\n\n<h2>XML</h2>\n\n<p>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.</p>\n\n<p>To the XML parser, a <code>script</code> tag is no better than any other tag. Particularly, a script node may contain non-text child nodes, triggered by "<code>&lt;</code>"; and a "<code>&amp;</code>" sign denotes a character entity.</p>\n\n<p>So, in XHTML, this is <em>not</em> possible:</p>\n\n<pre><code>&lt;script&gt;\nif (a&lt;b &amp;&amp; c&lt;d) {\n    alert(\'Hooray\');\n}\n&lt;/script&gt;\n</code></pre>\n\n<p>To work around this, you can wrap the whole script in a <code>CDATA</code> section. This tells the parser: \'In this section, <strong>don\'t treat "<code>&lt;</code>" and "<code>&amp;</code>" as control characters</strong>.\' To prevent the JavaScript engine from interpreting the "<code>&lt;![CDATA[</code>" and "<code>]]&gt;</code>" marks, you can wrap them in comments.</p>\n\n<p>If your script does not contain any "<code>&lt;</code>" or "<code>&amp;</code>", you don\'t need a <code>CDATA</code> section anyway.</p>\n'}, {'answer_id': 2358403, 'author': 'gehsekky', 'author_id': 21310, 'author_profile': 'https://Stackoverflow.com/users/21310', 'pm_score': 3, 'selected': False, 'text': '<p>to avoid xml errors during xhtml validation.</p>\n'}, {'answer_id': 2358405, 'author': 'Tyler Carter', 'author_id': 58088, 'author_profile': 'https://Stackoverflow.com/users/58088', 'pm_score': 2, 'selected': False, 'text': "<p>That way older browser don't parse the Javascript code and the page doesn't break.</p>\n\n<p>Backwards compatability. Gotta love it.</p>\n"}, {'answer_id': 2358406, 'author': 'Ikaso', 'author_id': 207959, 'author_profile': 'https://Stackoverflow.com/users/207959', 'pm_score': 3, 'selected': False, 'text': '<p>CDATA tells the browser to display the text as is and not to render it as an HTML.</p>\n'}, {'answer_id': 2358410, 'author': 'Franz', 'author_id': 192741, 'author_profile': 'https://Stackoverflow.com/users/192741', 'pm_score': 5, 'selected': False, 'text': "<p>It's an X(HT)ML thing. When you use symbols like <code>&lt;</code> and <code>&gt;</code> 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.</p>\n\n<p>The CDATA means that the following lines (everything up unto the <code>]]&gt;</code> is not XML and thus should not be parsed that way.</p>\n"}, {'answer_id': 2358411, 'author': 'Alex Beardsley', 'author_id': 14007, 'author_profile': 'https://Stackoverflow.com/users/14007', 'pm_score': 4, 'selected': False, 'text': '<p>CDATA indicates that the contents within are not XML.</p>\n\n<p>Here is an explanation on <a href="http://en.wikipedia.org/wiki/CDATA" rel="noreferrer">wikipedia</a></p>\n'}, {'answer_id': 2358418, 'author': 'LBushkin', 'author_id': 91671, 'author_profile': 'https://Stackoverflow.com/users/91671', 'pm_score': 4, 'selected': False, 'text': '<p><strong>It to ensure that XHTML validation works correctly when you have JavaScript embedded in your page, rather than externally referenced.</strong></p>\n\n<p>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.</p>\n\n<blockquote>\n  <blockquote>\n    <p>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.</p>\n    \n    <p>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). </p>\n  </blockquote>\n</blockquote>\n\n<p>You can learn <a href="http://en.wikipedia.org/wiki/CDATA" rel="noreferrer">more about CDATA here</a>, and <a href="http://en.wikipedia.org/wiki/XHTML" rel="noreferrer">more about XHTML here</a>.</p>\n'}, {'answer_id': 2358449, 'author': 'ondra', 'author_id': 149901, 'author_profile': 'https://Stackoverflow.com/users/149901', 'pm_score': 5, 'selected': False, 'text': "<p>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 &amp;,&lt;,> characters in the <em>script</em> tag and cause XML parsing error. So, you can write your JavaScript with entities, e.g.:</p>\n\n<pre><code>if (a &amp;gt; b) alert('hello world');\n</code></pre>\n\n<p>But this is impractical. The bigger problem is that if you read the page in HTML, the tag <em>script</em> 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 <em>script</em> tag in CDATA element in XHTML, but NOT to enclose it in HTML.</p>\n\n<p>This trick marks the start of a CDATA element as a 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.</p>\n"}, {'answer_id': 12149260, 'author': 'Paul Sweatte', 'author_id': 1113772, 'author_profile': 'https://Stackoverflow.com/users/1113772', 'pm_score': 3, 'selected': False, 'text': '<p>CDATA is necessary in any XML dialect, because text within an XML node is treated as a child element before being evaluated as JavaScript. This is also the reason why <a href="https://stackoverflow.com/questions/8019216/1113772/9881897#9881897">JSLint complains</a> about the <code>&lt;</code> character in regexes.</p>\n\n<p><strong>References</strong></p>\n\n<ul>\n<li><a href="http://www.ibm.com/developerworks/java/library/x-decxmlui/" rel="nofollow noreferrer">Creating a declarative XML UI language</a></li>\n<li><a href="http://xul.sourceforge.net/talk/mtd-may-2004/slides.html" rel="nofollow noreferrer">The Future of the Web: Rich Clients, Rich Browsers, Rich Portals</a></li>\n</ul>\n'}, {'answer_id': 12187566, 'author': 'Jim', 'author_id': 1634553, 'author_profile': 'https://Stackoverflow.com/users/1634553', 'pm_score': 3, 'selected': False, 'text': '<p>CDATA indicates that the contents within are not XML.</p>\n'}]
['https://Stackoverflow.com/questions/66837', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/208/']