Producing web pages
* HTML CSS DHTML XHTML A to Z of tags        Accessibility          Design

Scripts and styles need CDATA

If you use the <script> or <style> tags on your page, you need to put the content of those tags in a CDATA section. If you don't, the browser sees characters like < in your code as the start of a tag.

So, the point of CDATA sections is to tell a browser to ignore characters that it would otherwise see as markup. The only delimiter that is recognized in a CDATA is the "]]>" string which ends the CDATA section.

None of this applies if you:

  • Load an external script file. For example, <script language="JavaScript" src="expand.js">
  • Load an external CSS file. For example, <link rel="Stylesheet" href="htmlhelp.css" type="text/css">

It applies when you put code on the page. For example

<script language="JavaScript">
<!--
<![CDATA[
function mouseon() {
    var el = event.srcElement;
    var thisclass = event.srcElement.className;
    if (thisclass=="parent") {
        el.style.color = "tomato";
    }
  }

. . ]]> //--> </script>