Why use *//<![CDATA[* and *//]]>* in a jQuery script?

javascript, jquery

Solution

CDATA is used to allow the document to be loaded as straight XML. You can embed JS in XML documents without replacing special XML characters like <, >, &, etc by XML entities &lt;, &gt;, &amp; etc to prevent that the XML syntax get corrupted.

So `double slash //` in your XML will be treated as text instead of a comment and hence it makes CDATA as an XML tag.

The wiki says that:-

In an XML document or external parsed entity, a CDATA section is a section of element content that is marked for the parser to interpret as only character data, not markup. A CDATA section is merely an alternative syntax for expressing character data; there is no semantic difference between character data that manifests as a CDATA section and character data that manifests as in the usual syntax in which < and & would be represented by < and &, respectively.

Problem

I have this working piece of javascript: ``` <script type="text/javascript"> //<![CDATA[ jQuery(document).ready(function() { jQuery("#page_template option[value='sidebar-page.php']").remove(); }); //]]> </script> ``` What's `//<![CDATA[` and `//]]>` stand for? I never used it but lately I meet it very often. Thank you guys in advance for increasing my knowledge! ;)

Original source

Related problems