Came across an interesting problem today, whilst creating a template for a new website, I noticed that the Google analytics tracking code was failing XHTML validation.

<script type=”text/javascript” language=”javascript”>
if(typeof(urchinTracker)!=’function’)document.write(‘<sc’+’ript src=”‘+’http’+(document.location.protocol==’https:’?’s://ssl’:’://www’)+’.google-analytics.com/urchin.js’+'”><‘+’/sc’+’ript>’)
</script>

I later discovered that wrapping the script in CDATA tags allowed the script to parse and validate – happy days!

<script type=”text/javascript” language=”javascript”>
//<![CDATA[if(typeof(urchinTracker)!=’function’)document.write(‘<sc’+’ript src=”‘+’http’+(document.location.protocol==’https:’?’s://ssl’:’://www’)+’.google-analytics.com/urchin.js’+'”><‘+’/sc’+’ript>’)//]]>
</script>