Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Søren S 2 posts 22 karma points
    Oct 22, 2009 @ 16:05
    Søren S
    0

    <script> in umbraco pages

    Hi.
    I've tried for too long now to let a couple of simple script tags come through to the html out put and need an advice.


    I can manage to let it out by adding the html to the masterpage template and then it works, but every other try else seems hard.
    What I want is to have the option of a unique script for every page I create....

    I've read about a similar problem with youtupe inline scripts, where the solution is to create an xslt file and a connecting macro.

    this is somewhat I want to add to the pages:

    <script type="text/javascript"><!--
    google_ad_client = "pub-1494612478867990";
    /* 160x600, oprettet 14-10-09 */
    google_ad_slot = "4712153794";
    google_ad_width = 160;
    google_ad_height = 600;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    

    but with this

    My xslt first try:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxml="urn:schemas-microsoft-com:xslt"
     xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
     exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <xsl:variable name="_ad_client" select="/macro/_ad_client"/>
    <xsl:variable name="_ad_slot" select="/macro/_ad_slot"/>
    <xsl:variable name="_ad_width" select="/macro/_ad_width"/>
    <xsl:variable name="_ad_height" select="/macro/_ad_height"/>
    <!-- start writing XSLT -->
    <script type="text/javascript"><!--
    <xsl:attribute name="google_ad_client"><xsl:value-of select="$_ad_client" /></xsl:attribute>
    /* 160x600, oprettet 14-10-09 */
    <xsl:attribute name="google_ad_slot"><xsl:value-of select="$_ad_slot" /></xsl:attribute>
    <xsl:attribute name="google_ad_width"><xsl:value-of select="$_ad_width" /></xsl:attribute>
    <xsl:attribute name="google_ad_height"><xsl:value-of select="$_ad_height" /></xsl:attribute>
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    </xsl:template>
    </xsl:stylesheet>



     

    plus 4 macros with
    type text and names and aliases:
    "_ad_client"
    "_ad_slot"
    "_ad_width"
    "_ad_height"

    then this is what it html outputs:

    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    


    Is xslt the way to solve this?
    why isn't the solution a simple macro, where the data of a textfield in one parameter is just printet raw?

    Thanks in advance and best regards.
    Soren.

  • dandrayne 1138 posts 2262 karma points
    Oct 22, 2009 @ 16:14
    dandrayne
    1

    You need to use cdata for your scripts

    Here's an example from a macro of mine

    <script type="text/javascript">
    <![CDATA[

    var path = "]]><xsl:value-of select="$videopath" /><![CDATA[";
    var videowidth = "]]><xsl:value-of select="$videowidth" /><![CDATA[";
    var videoheight = "]]><xsl:value-of select="$videoheight" /><![CDATA[";
    var flashvars = {};
    var attributes = {};
    var params = {
      wmode
    : "transparent",
      allowfullscreen
    : "true",
      allscriptaccess
    : "always"
    };

    swfobject
    .embedSWF(path,"flashLayer", videowidth, videoheight, "9.0.0", flashvars, params, attributes);

    ]]>
    </script>

    This shows the CDATA, and how to access xslt "variables" inside js

    Dan

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 22, 2009 @ 22:07
    Chriztian Steinmeier
    0

    Hi Søren,

    This is just to help understand why your XSLT isn't working the way you think it should.

    XSLT is basically just an XML document, and in XML you write a comment just like you would in HTML. So effectively, you've commented that section of your XSLT out and hidden it from the processor, which is why your output only contains the script tags.

    What you really want to do is to generate a comment in the output, which is done with the <xsl:comment> instruction:

    <!-- Hey, I'm a comment in the code -->
    <xsl:comment>Hey, I will be a generated comment</xsl:comment>

    So you'd do something like this instead:

    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
    
        <xsl:template match="/">
            <xsl:call-template name="write-googlescript" />
        </xsl:template>
    
        <xsl:template name="write-googlescript">
            <xsl:variable name="ad_client" select="/macro/_ad_client"/>
            <xsl:variable name="ad_slot" select="/macro/_ad_slot"/>
            <xsl:variable name="ad_width" select="/macro/_ad_width"/>
            <xsl:variable name="ad_height" select="/macro/_ad_height"/>
            <script type="text/javascript"><xsl:comment>
                google_ad_client = "<xsl:value-of select="$ad_client" />";
                /* 160x600, oprettet 14-10-09 */
                google_ad_slot = "<xsl:value-of select="$ad_slot" />";
                google_ad_width = <xsl:value-of select="$ad_width" />;
                google_ad_height = <xsl:value-of select="$ad_height" />;
                //</xsl:comment>
                <xsl:text>&#x0a;</xsl:text>
            </script>
            <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><xsl:comment /></script>
        </xsl:template>
    
    </xsl:stylesheet>

    - I put the variables inside the template so it's possible to call the template with overriding parameters, e.g., to override the width & height but keep the client & slot:

    <xsl:call-template name="write-googlescript">
        <xsl:with-param name="ad_width" select="180" />
        <xsl:with-param name="ad_height" select="180" />
    </xsl:call-template>

    /Chriztian

  • Søren S 2 posts 22 karma points
    Oct 23, 2009 @ 15:48
    Søren S
    0

    Works great.
    Thanks a lot, I appreciate it.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 23, 2009 @ 22:09
    Jan Skovgaard
    0

    +1 one for Dan's soultion, which I would at any time consider the most simple and elegant. The other way of doing it seems to cumbersome and confusing. But of course you can have a reason for choosing to do it that way. But maybe you should consider simplyfying it for your own benefit?

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft