Archive for the ‘(X)HTML’ Category

JavaScript Does Not Validate

Tuesday, October 4th, 2005

Remember to put CDATA tags around the actual JavaScript code if it uses < or &. See Script and Style elements at the W3C. For example, use:

<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>

Actually, the above does not work correctly in Mozilla browers such as Firefox. See Properly Using CSS and JavaScript in XHTML Documents. They recommend putting everything in external files. If that’s not possible, use a modification of the above code:

<script type="text/javascript">
//<![CDATA[
... unescaped script content ...
//]]>
</script>

Useful Validation Links: