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

We agree that magento use the CDATA in javascript pieces codes, but I would like to know which is really the utility of this. There are some who say that we should put it in the XHTML and XML. So for exemple in some phtml, if i need to add a javascript function to validate some field or add a class etc ... i should add the CDATA between the code or not necessarily.

1 Answer 1

3

I found also this answer:

It to ensure that XHTML validation works correctly when you have JavaScript embedded in your page, rather than externally referenced.

XHTML requires that your page strictly conform to XML markup requirements. Since JavaScript may contain characters with special meaning, you must wrap it in CDATA to ensure that validation does not flag it as malformed.

With HTML pages on the web you can just include the required JavaScript between and tags. When you validate the HTML on your web page the JavaScript content is considered to be CDATA (character data) that is therefore ignored by the validator. The same is not true if you follow the more recent XHTML standards in setting up your web page. With XHTML the code between the script tags is considered to be PCDATA (parsed character data) which is therefore processed by the validator.

Because of this, you can't just include JavaScript between the script tags on your page without 'breaking' your web page (at least as far as the validator is concerned).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.