Re: cdata, javascript and xhtml1.1

Pretty simple why <![CDATA[ ... ]]> exists

look at the following code:

<script type="text/javascript">
	function x()
	{
		return 2 < 1 // returns false
	}
</script>

The function won't work, becose there is a less-than ("<") sign which says
to the parser, here a new tag begins. Well you already guessed that this is
not what you really want, becose you expect it to be all javascript. Well
the solution is <![CDATA[ ... ]]>

look at the changed code:

<script type="text/javascript"><![CDATA[
	function x()
	{
		return 2 < 1 // returns false
	}
]]></script>

If you do this, the < will be interpret as a real less-than sign, not as e
tag start character. Note that IE seems to have problems with <![CDATA[
... ]]>, this is easy to solve:

<script type="text/javascript">//<![CDATA[
	function x()
	{
		return 2 < 1 // returns false
	}
//]]></script>

While this is still correct XHTML, you'll fool the browser with using
comment notation.

Sjoerd van Leent

Received on Thursday, 31 July 2003 03:54:30 UTC