Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  11] [ 5]  / answers: 1 / hits: 194844  / 13 Years ago, wed, august 17, 2011, 12:00:00

What is the use of CDATA inside JavaScript tags and HTML?



<script type=text/javascript> 
// <![CDATA[

// ]]>
</script>

More From » html

 Answers
28

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



The term CDATA is used about text data that should not be parsed by the XML parser.


Characters like "<" and "&" are illegal in XML elements.


"<" will generate an error because the parser interprets it as the start of a new element.


"&" will generate an error because the parser interprets it as the start of an character entity.


Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.


Everything inside a CDATA section is ignored by the parser.


A CDATA section starts with "<![CDATA[" and ends with "]]>"



Use of CDATA in program output



CDATA sections in XHTML documents are liable to be parsed differently by web browsers if they render the document as HTML, since HTML parsers do not recognise the CDATA start and end markers, nor do they recognise HTML entity references such as &lt; within <script> tags. This can cause rendering problems in web browsers and can lead to cross-site scripting vulnerabilities if used to display data from untrusted sources, since the two kinds of parsers will disagree on where the CDATA section ends.



A brief SGML tutorial.


Also, see the Wikipedia entry on CDATA.


[#90567] Monday, August 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sandra

Total Points: 708
Total Questions: 100
Total Answers: 84

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
sandra questions
Tue, Jun 30, 20, 00:00, 4 Years ago
Sun, May 31, 20, 00:00, 4 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Fri, May 31, 19, 00:00, 5 Years ago
;