Copied to clipboard

Flag this post as spam?

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


  • bev0 39 posts 59 karma points
    Jun 27, 2012 @ 20:44
    bev0
    0

    call xsl variable inside javascript

    I have a xsl variable which need to be call inside a javascript, how can this be done ? i've tried varies of way but still no luck. please help.  here is an example.

    on this line
    window.location = 'http://server-name/mobilemoviedetail.aspx?movieID=-2';

    i want to replace the "-2" to <xsl:value-of select="$movieID"/>

     

    <?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" xmlns:LocationSearchXslt="urn:LocationSearchXslt" xmlns:umbraco.contour="urn:umbraco.contour" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets  LocationSearchXslt umbraco.contour ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template match="/">
      
      <xsl:variable name="movieID">
        <xsl:choose>
          <xsl:when test="contains(umbraco.library:RequestServerVariables('URL'),'movie-detail.aspx') and string-length(umbraco.library:RequestQueryString('node_id'))>0">
            <xsl:value-of select="umbraco.library:GetXmlNodeById(umbraco.library:RequestQueryString('node_id'))/movie_id"/>
          </xsl:when>
          <xsl:otherwise>-1</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:choose>
        <xsl:when test="string-length(umbraco.library:Split(umbraco.library:RequestCookies('sitetype'),',')/value) = 0">
          <script type="text/javascript">
            var agentID = navigator.userAgent.toLowerCase().match(/(android|webos|iphone|ipad|blackberry|avantgo|blazer|compal|elaine|hiptop|iris|kindle|lge|maemo|midp|mmp|mobile|o2|opera|palm)/);
            if (agentID) 
            
              if (document.cookie.indexOf("iPhoneRedirect")==-1 <xsl:value-of select="'&amp;&amp;'" disable-output-escaping="yes"/> (navigator.userAgent.toLowerCase().match(/iphone/i) || navigator.userAgent.toLowerCase().match(/ipod/i) || navigator.userAgent.toLowerCase().match(/ipad/i))) window.location = "http://server-name/mobilesplashiphone";          
              else if (document.cookie.indexOf("AndroidRedirect") == -1 <xsl:value-of select="'&amp;&amp;'" disable-output-escaping="yes"/> (navigator.userAgent.toLowerCase().match(/android/i))) window.location = "http://server-name/mobilesplashandroid";
              else if (window.location.href.indexOf('movie-detail.aspx')!=-1) 
              {
                window.location = 'http://server-name/mobilemoviedetail.aspx?movieID=-2';
              }        
              else if (window.location.href.indexOf('weekly-email.aspx?status=unsubscribed')!=-1) 
              {
                var lURL = String(window.location.href);
                var lPos = lURL.indexOf('?');      
                window.location = 'http://server-name/mobileweeklyemail.aspx'+lURL.substr(lPos,(lURL.length-lPos));
              }
              else if (window.location.href.indexOf('movie-detail.aspx')==-1) window.location = 'http://server-name/mobilehome.aspx';

            }
          </script>
        </xsl:when>

      </xsl:choose>
          
    </xsl:template>

    </xsl:stylesheet>

     

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jun 27, 2012 @ 20:57
    Chriztian Steinmeier
    0

    Hi bev0,

    You can just do a <xsl:value-of /> where you want it - you should look into using a CDATA Section for all the JavaScript, so you don't have to escape stuff, e.g.:

          <script type="text/javascript"><![CDATA[
            var agentID = navigator.userAgent.toLowerCase().match(/(android|webos|iphone|ipad|blackberry|avantgo|blazer|compal|elaine|hiptop|iris|kindle|lge|maemo|midp|mmp|mobile|o2|opera|palm)/);
            if (agentID) 
            { 
              if (document.cookie.indexOf("iPhoneRedirect")==-1 && (navigator.userAgent.toLowerCase().match(/iphone/i) || navigator.userAgent.toLowerCase().match(/ipod/i) || navigator.userAgent.toLowerCase().match(/ipad/i))) window.location = "http://server-name/mobilesplashiphone";          
              else if (document.cookie.indexOf("AndroidRedirect") == -1 && (navigator.userAgent.toLowerCase().match(/android/i))) window.location = "http://server-name/mobilesplashandroid";
              else if (window.location.href.indexOf('movie-detail.aspx')!=-1) 
              {
                window.location = 'http://server-name/mobilemoviedetail.aspx?movieID=]]><xsl:value-of select="$movieID" /><![CDATA[';
              }        
              else if (window.location.href.indexOf('weekly-email.aspx?status=unsubscribed')!=-1) 
              {
                var lURL = String(window.location.href);
                var lPos = lURL.indexOf('?');      
                window.location = 'http://server-name/mobileweeklyemail.aspx'+lURL.substr(lPos,(lURL.length-lPos));
              }
              else if (window.location.href.indexOf('movie-detail.aspx')==-1) window.location = 'http://server-name/mobilehome.aspx';
            }]]>
          </script>
    
    I will say, however, that a much better solution will be to put the JS functionality into a function in a separate file, and just generate a simple function call in the XSLT.

     

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft