I sometimes notice a CSS or JS code surrounded with <![CDATA[
and ]]>
tags. I couldn't find any explanation to this. What does CDATA do? Why is it used by some people and what is it needed for?
A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.
CDATA is necessary in any XML dialect, because text within an XML node is treated as a child element before being evaluated as JavaScript.
A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &. The only markup an XML pro-cessor recognizes inside a CDATA section is the closing character sequence ]>.
When you write inline javascript or CSS in an HTML file (using the script and style tags), Those CDATA comments prevent things like the HTML validators from catching false positives when validating an HTML document. Copy link CC BY-SA 2.5. Follow this answer to receive notifications.
It tells the interpreter that it should not try to interpret the data enclosed in the tags. For example, if you want a XML file to contain a comment with < or >, XML interpreters will report the file as invalid because the caracters < and > will not be part of standard tag. You simply have to surround the code with the CDATA tags.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With