Sign In/My Account | View Cart  
advertisement

Article:
 XSLT, Browsers, and JavaScript
Subject: Scripts need CDATA too
Date: 2003-02-13 07:05:51
From: Taylor Cowan

Bob, I want to add that we need to use CDATA with larger scripts, and any non-xml text in general.
This would cause a problem:


<script language="JavaScript1.2">
function showWhyWeNeedCdata {
if ( 1 < 2 )
alert("scripts need CDATA");
}
</script>


Yours were fine, but we run across <,>, and sometimes & in scripts.


Taylor Cowan


Previous Message Previous Message   Next Message Next Message


Titles Only Full Threads Newest First
  • 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>



Sponsored By: