Scripts need CDATA too
2003-02-13 11:47:34 Bob DuCharme
[Reply]
I find that nearly all uses of CDATA are kludges. Here are some other things you can do:
1. Use entity references for those characters. I've pasted a script below that worked with Mozilla 1.2.1 and IE 6.0 under Win98.
2. Put the script in a separate file.
(The latter choice may not be an option if the script makes use of data from the source document, which is something I'll demonstrate in my next column.)
Bob
<!-- load the following short document into your browser as an XML file -->
<?xml-stylesheet href="test1.xsl" type="text/xsl" ?>
<x/>
<!-- here is test1.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head>
<title>test</title>
<script language="JavaScript1.2">
function showWhyWeNeedCdata() {
if ( 1 < 2 )
alert("scripts rarely need CDATA");
}
</script>
</head>
<body><form>
<input type="button" value=" go " onClick="showWhyWeNeedCdata()"/>
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>