JavaScript with XHTML
Wednesday, 4 October 2006
Having recently ported some HTML pages to XHTML, we were a little surprised that our favourite validator continued to warn us that our very simple one-line javascript code was no longer correct:
<script language="javascript" type="text/javascript">
copthis();
</script>
Apparently, the trick is to enclose the actual javascript code with a “CDATA” section like this:
<script language="javascript" type="text/javascript">
//<![CDATA[
copthis();
//]]>
</script>
The reason is apparently that under the XHTML standard, the javascript code is considered as “PCDATA” and therefore treated by the validator in the same manner as the rest of the markup code. See this article from About.com for more.
|