Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of CDATA

Tags:

jquery

xml

cdata

Gurus,

I am self taught. There's a lot of what you enlightened ones call basic I know nothing about.

Reading this jQuery Tutorial, I noticed this tag (for lack of better word): "CDATA" as shown here (third line from the top):

<script src="http://jquery.com/src/jquery-latest.js"></script>
<script> 
//<![CDATA[    
$(document).ready(function(){
$(".article .thebody").hide();
$("#container .article ul")
       .prepend("<li class='readbody'><a href='' title='Read the article'>Read Body</a></li>");

$(".actions li.readbody a").click(function(event){
$(this).parents("ul").prev(".thebody").toggle();
event.preventDefault();
     });
   });
//]]></script>

What is the meaning of CDATA? Are there tags similar to CDATA?

like image 862
Chris Avatar asked Dec 02 '10 20:12

Chris


People also ask

Why do we need CDATA?

By using CDATA section, you are commanding the parser that the particular section of the document contains no markup and should be treated as regular text.

Should I use CDATA?

You should almost never need to use CDATA Sections. The CDATA mechanism was designed to let an author quote fragments of text containing markup characters (the open-angle-bracket and the ampersand), for example when documenting XML (this FAQ uses CDATA Sections quite a lot, for obvious reasons).

How can you declare CDATA?

You declare a CDATA section using <! [CDATA[ as the opening tag, and ]]> as the closing tag.

What is CDATA in Java?

CDATA sections are used to escape blocks of text containing characters that would otherwise be regarded as markup. The only delimiter that is recognized in a CDATA section is the "]]>" string that ends the CDATA section. CDATA sections cannot be nested.


1 Answers

With <![CDATA[ you can embed JS in XML (and XHTML) documents without the need to replace special XML characters like <, >, &, etc by XML entities &lt;, &gt;, &amp; etc to prevent that the XML syntax get malformed and that you get errors like The entity name must immediately follow the '&' in the entity reference. The general recommendation is however to put JS code in its own .js file which you then include by a <script src>.

The <![CDATA[ is not needed in plain HTML documents. Unless you're developing with a XML based view technology like Facelets (for JSF) or ASP.NET MVC, there's absolutely no need to declare your HTML as XHTML. Just a <!DOCTYPE html> would suffice

like image 73
BalusC Avatar answered Oct 02 '22 23:10

BalusC