What is the difference between using [CDATA []] inside a tag script?

9

What is the difference between using or not using the script contents with this CDATA?

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

   ...

//]]
</script>
    
asked by anonymous 11.12.2015 / 17:34

2 answers

2

Sometimes, within the XML syntax, developers will place a sequence of entries between character data, or CDATA . The information that is placed between these media is unrecognizable to the XML parser. The motivation for using CDATA is to place special notes within the code or include illegal characters such as commercial or "< >" which normally cause the XML parser to crash (crash in the XML parser). If such symbols are essential for input, then the application of CDATA is a must.

    
11.12.2015 / 18:29
5

From the Javascript perspective, this CDATA does nothing because it is within a comment. It's there as an HTML to turn a valid XML document apart from being simply an HTML document.

For example the following script is not valid XML because the tag " </blah> " is not balanced. (Remember that from the XML viewpoint, " // " is not a comment.)

<script type="text/javascript">
    //</blah>
</script>

That said, in practice this CDATA is useless since HTML is not and need not be XML. For example, HTML <x/> is not equivalent to <x></x> . In fact. <x/> is equal to <x> , with / being simply ignored.

Furthermore, you do not need to type="text/javascript" . The original idea of this attribute was to allow different scripting languages but this is also another thing that did not avenge.

<script>
  //...
</script>
    
11.12.2015 / 18:22