This question already has an answer here:
What is the use of CDATA inside JavaScript tags and HTML?
<script type="text/javascript">
// <![CDATA[
// ]]>
</script>
This question already has an answer here:
What is the use of CDATA inside JavaScript tags and HTML?
<script type="text/javascript">
// <![CDATA[
// ]]>
</script>
CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows: Replace character entities with characters,
Ignore line feeds,
Replace each carriage return or tab with a single space.
CDATA
has no meaning at all in HTML.CDATA
is an XML construct which sets a tag's contents that is normally #PCDATA - parsed character data, to be instead taken as #CDATA, that is, non-parsed character data. It is only relevant and valid in XHTML.It is used in
script
tags to avoid parsing<
and&
. In HTML, this is not needed, because in HTML,script
is already #CDATA.From http://en.wikipedia.org/wiki/CDATA:
CDATA is Deprecated.
So do not use it in HTML 5.
https://developer.mozilla.org/en-US/docs/Web/API/CDATASection
A way to write a common subset of HTML and XHTML, in the hope of greater portability.
In HTML,
<script>
is magic escapes everything until</script>
appears.So you can write:
and
<br/>
won't be considered a tag.This is why strings such as:
must be escaped like:
See: Why split the <script> tag when writing it with document.write()?
But XML (and thus XHTML, which is a "subset" of XML, unlike HTML), doesn't have that magic:
<br/>
would be seen as a tag.<![CDATA[
is the XHTML way to say:The
//
is added to make the CDATA work well in HTML as well.In HTML
<![CDATA[
is not magic, so it would be run by JavaScript. So//
is used to comment it out.The XHTML also sees the
//
, but will observe it as an empty comment line which is not a problem:That said:
<!DOCTYPE html>
vs<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
script
syntaxBut that violates the golden rule of the Internet:
All text in an XML document will be parsed by the parser.
But text inside a CDATA section will be ignored by the parser.
CDATA - (Unparsed) Character Data
Use of CDATA in program output
A brief SGML tutorial.
Also, see the Wikipedia entry on CDATA.