Search

I've just put a Symphony site up on a Textdrive server and ran into a problem with the XSLT processor automatically adding CDATA elements immediately inside the script tags. That is, the following code:

<script type="text/javascript">
  alert('JavaScript FTW');
</script>

would output this:

<script type="text/javascript"><![CDATA[
  alert('JavaScript FTW');
]]></script>

As this lacks any JS comments, the browser chokes trying to run the function and chaos ensues. I guess ideally you would simply disable that in the XSLT processor, but as this is on shared hosting I don't have access. Anyway, I muddled around for a little while trying to find a solution, so I thought I'd put it up here in case anyone runs into the same problem. To get it working you need to do the following:

<xsl:text disable-output-escaping="yes"><![CDATA[
<script type="text/javascript">
//<![CDATA[
  alert('JavaScript FTW');
//]]>]]&gt;
&lt;/script>
</xsl:text>

It feels pretty dirty, but it works.

no need for such dirtyness. simply do this:

<script type="text/javascript">
  <xsl:comment>
    alert('JavaScript Not FTW!');
  //</xsl:comment>
</script>

which then results in this:

<script type="text/javascript">
  <!--
    alert('JavaScript Not FTW!');
  //-->
</script>

there are a bunch of threads about this on the other overture forum, but not sure where.

Wrong thread.

as mentioned here there really is a need for a tutorial on this. This also clearly states that any inline JS is evil, and if used, using comments around it is also evil....

not really sure why a tutorial is needed for this. look at my post above, that accomplishes what you need to get inline js to work in your templates.

There are occasions when you need to use inline JavaScript — such as when you need to dynamically created the JS itself injecting values using XSLT. With the example above using xsl:comment can one do the following?

<script type="text/javascript">
    <xsl:comment>
        alert('<xsl:value-of select="blog/entry/title"/>');
    //</xsl:comment>
</script>

Hi Nick,

yes.

Hi Fazal,

thanks.

You two are weird.

wdtan, 'getting it work' doesnt satisfy most symphonians doesnt it, we wont to do it the proper way. Using comments some browsers will not see the JS, and you cannot use double dashes. And as Nick says, ofcourse we want to dynamically create our JS. Therefore I would prefer to dynamically created external JS files, like Michael-e has done.

Should this work???

<script type="text/javascript">
  <xsl:comment>
    $(function(){ 
    $('input[title!=""]').hint();
    });
  //</xsl:comment>
</script>

Because it doesn’t run the script. I have the js here in my head:

<script src="{$root}/workspace/js/clear.js" type="text/javascript"></script>

What’s the rendered HTML for your script tag?

    <script type=”text/javascript”><!–
    $(function(){ 
    $(‘input[title!=”“]’).hint();
    });
  //–></script>

I had to pass some parameters to a Flash embed and had trouble getting things to work too. I ended up doing something completely gross, but it worked. I’d love to learn a better way to achieve this.

                <script type="text/javascript">        
                    <![CDATA[
                    var so = new SWFObject("]]><xsl:value-of select="$workspace"/><xsl:text>/swf/gallery.swf</xsl:text><![CDATA[", "slideshow", "590", "384", "8", "#ffffff");
                    so.addParam("FlashVars", "xmlfile=]]><xsl:value-of select="$root"/><xsl:text>/</xsl:text><xsl:value-of select="$root-page"/><xsl:text>/images/</xsl:text><xsl:value-of select="portfolio-entry/entry/title/@handle"/><xsl:text>/</xsl:text><![CDATA[&xmlfiletype=Default");
                    so.addParam("wmode", "transparent");
                    so.write("flashcontent");
                    ]]>
                </script>

That script works as long as each js statement is on one line. It’s an embed script for a Slideshow Pro SWF that links to an XML page that has all the images for that particular entry. I have to pass page specific parameters to the SWF so I can set it up so every entry in that section can use the same SWF file but link to a specific XML page to link the images.

Well this works:

<script type="text/javascript">
  <xsl:comment>
    alert('JavaScript Not FTW!');
  //</xsl:comment>
</script>

So I don’t understand what I am doing differently.

Hi there,

I'm new around here and new to XSL and Symphony. I'm having an issue with the <![CDATA[ ]]> and I'm wondering was is the logic behind all this.

Why does the parser changes my > into &gt;, and what should I do in this case ?

<script type="text/javascript"> <![CDATA[ if ( $('#visus').children().length > 0 ) { DO SOMETHING }; ]]> </script>

Thanks for your lights on this.

Hi antoine,

simply swap your > for &gt;. When your stylesheet is transformed XSL will change this back to the desired output.

Hi Fazal, well I have tryed that (I should have mentioned it).

If I simply swap the > for &gt; then it stays that ways, and the javascript breaks. If I add the <![CDATA[ ]]> markup arount my script and replace > for &gt; then it becomes: &amp;gt;.

That is why I'm wondering what is the logic. Fortunatly, I can make my script work with !=, but I'm still curious for next time.

i know this is kind of a stereotypical jerk response, but have you considered abstracting your javascript? mixing your code in with the presentation layer (the xslt pages) can cause confusion and troubles, especially for anybody who has to work with your site later. in the instance of xslt, it's also very convenient because you don't have to worry about issues like this.

I'm not sure I get what you are saying, but I think you mean doing a <xsl:if> instead of a jasvascript if. If so, well, I didn't thought about that but that should work, thanks,

but still did anyone had the same issue? Shouldn't the CDATA tag alow for any javascript?

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details