9
What is the point, or what is the difference of using or not the content of the script with this CDATA?
<script type="text/javascript">
//<![CDATA
...
//]]
</script>
9
What is the point, or what is the difference of using or not the content of the script with this CDATA?
<script type="text/javascript">
//<![CDATA
...
//]]
</script>
5
From the Javascript point of view, this CDATA does nothing because it is inside a comment. It is there as an HTML to become a valid XML document besides 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 point of view, "//
" is not a comment).
<script type="text/javascript">
//</blah>
</script>
That being said, in practice this CDATA is useless since HTML is not and does not need to be XML. For example, in HTML <x/>
is not equivalent to <x></x>
. In fact. <x/>
amounts to <x>
, with the /
being simply ignored.
Besides, you don’t have to put the type="text/javascript"
. The original idea of this attribute was to allow different scripting languages but this is also something else that did not succeed.
<script>
//...
</script>
thanks, I think your comment plus the @durtto clarifies why it is sometimes necessary.
Just to be clear, you have a 99% chance of not being needed these days. It will only make a difference if your system is using XML tools to manipulate your HTML. From the browser’s point of view CDATA is totally unnecessary.
@hugomg has avenged yes, you’ve heard of Coffeescript? <script type="text/coffeescript"></script>
In fact, any "type=" other than "text/javascript" is completely ignored by the browser. In your case you have a javascript script that goes through all these tags marked with text/cofeescript and runs the cofeescript interpreter in their content.
2
Sometimes, within XML syntax, developers will place a sequence of entries between character data, or supports CDATA
. The information that is placed between these media is unrecognizable to the XML parser. The motivation for using the CDATA
, is to place special notes within the code or include illegal characters such as commercial or "<>" that usually cause XML parser crash (crash in XML parser). If such symbols are essential to the input, then the application of the CDATA
is an obligation.
makes sense, because sometimes javascript didn’t work without CDATA
Browser other questions tagged javascript html xml xhtml
You are not signed in. Login or sign up in order to post.
No need to apologize, you’re not stupid for not knowing something.
– Laerte
Related: What is XML CDATA for?.
– Renan Gomes