Ever see that CDATA stuff in JavaScript and wonder what it was for? It is to prevent the validator from reading that code as markup and failing validation. For example, ampersands (&) and greater-than / less-than signs (< >)are often culprits for failing validation when it’s actually perfectly OK to use them in JavaScript.
<script type="text/javascript">
//<![CDATA[
alert("<This is compatible with XHTML>");
//]]>
</script>
CDATA is a good friend of mine :)
I always assumed that was for older browsers, good to know its actual meaning.
I had this doubt some time ago an googled it to find out.
Thank you! I always did wonder. Now I’ll use it!
I did wonder that! I’m also liking the site’s new look. I haven’t been here for a while and it’s totally different!
=), thx chris for this hint…I had this issue several times (not validation xhtml because of markup in the js btw. elements that were appended and were hard-coded in the js)…I just outplaced the code in an external .js file and the error has gone but now I know what to do next time!
I think this snippet is a little misleading. The use of “validating” makes it sound like marking blocks as CDATA is only useful if you “want your pages to validate.” Whereas validate in this case actually means, “if you want your pages to render properly.”
This is one of those facepalm moments for me, I’m afraid. I had precisely this problem not long ago, with embedded JavaScript causing a load of validation errors.
I would up moving it to an external .js file, which worked better anyway, but next time I’ll know! Thanks Chris!