JavaScript with XHTML

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.

Due to the large volume of spam, comments are disabled. If you have anything relevant to say, you can leave a , or contact me directly.

About the author

Ivan's mugshotI'm Ivan Lutrov and I'm the owner of Lutrov Interactive. I have 25 years of experience producing interactive work and I create cost effective business websites that are simple, engaging and easy to use. I practice what I preach and I say what I really think, even if it's sometimes not what you want to hear. Subscribe to the Lutrov Interactive feed via RSS and follow me on Twitter.