Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a CDATA section necessary within a script tag?

Are CDATA tags ever necessary in script tags and if so when?

In other words, when and where is this:

<script type="text/javascript"> //<![CDATA[ ...code... //]]> </script> 

preferable to this:

<script type="text/javascript"> ...code... </script> 
like image 450
brad Avatar asked Sep 15 '08 20:09

brad


People also ask

What is the use of CDATA in JavaScript?

A CDATA section contains text that will NOT be parsed by a parser. Tags inside a CDATA section will NOT be treated as markup and entities will not be expanded. The primary purpose is for including material such as XML fragments, without needing to escape all the delimiters.

What is CDATA section in HTML?

HTMLWeb DevelopmentFront End Technology. The CDATA Section interface is used within XML for including extended portions of text. This text is unescaped text, like < and & symbols. These symbols do not want to escape.

Why is CDATA used?

Climate Data Analysis Tools (CDAT) helps in processing and visualization of climate model data. In the present study a modeling tool is developed by python programming language, for retrieving the climate grid point data. The retrieved data is clipped for specific area of interest using shapefile of the area.

What are CDATA tags?

CDATA stands for Character Data. You can use this to escape some characters which otherwise will be treated as regular XML. The data inside this will not be parsed. For example, if you want to pass a URL that contains & in it, you can use CDATA to do it.


1 Answers

A CDATA section is required if you need your document to parse as XML (e.g. when an XHTML page is interpreted as XML) and you want to be able to write literal i<10 and a && b instead of i&lt;10 and a &amp;&amp; b, as XHTML will parse the JavaScript code as parsed character data as opposed to character data by default. This is not an issue with scripts that are stored in external source files, but for any inline JavaScript in XHTML you will probably want to use a CDATA section.

Note that many XHTML pages were never intended to be parsed as XML in which case this will not be an issue.

For a good writeup on the subject, see https://web.archive.org/web/20140304083226/http://javascript.about.com/library/blxhtml.htm

like image 144
Michael Ridley Avatar answered Sep 28 '22 06:09

Michael Ridley