Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass a xsl variable value to a javascript function

i am trying to pass an xsl variable value to a javascript function.

My xsl variable

<xsl:variable name="title" select="TITLE" />

i'm passing the value like this

<input type="button" value="view" onclick="javascript:openPage('review.html?review=$title')" />

i have tried the above code in different possible ways but i gets errors.

<script type="text/javascript">
                    function jsV() {
                    var jsVar = '<xsl:value-of select="TITLE"/>';
                    return jsVar;
                    }
                </script>

                <input type="button" value="view" onclick="javascript:openPage('javascript:jsV()')" />

I also tried 

<input type="button" value="view" onclick="javascript:openPage('review.html?review='\''
    +$title+'\')" />

Is there alternative way or am i not doing it right?

like image 216
Govnah Avatar asked May 29 '11 09:05

Govnah


2 Answers

You forgot about {}:

<input type="button" value="view" onclick="javascript:openPage('review.html?review={$title}')" />
like image 97
sergzach Avatar answered Sep 21 '22 23:09

sergzach


Here is a working example how to do this:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
   <xsl:variable name="vTitle" select="TITLE"/>

     <input type="button" value="view"
     onclick="javascript:openPage('review.html?review={$vTitle}')" />
 </xsl:template>

</xsl:stylesheet>

when applied on this XML document (no XML document was provided!):

<contents>
 <TITLE>I am a title</TITLE>
</contents>

produces the wanted, correct result:

<input type="button" value="view" 
 onclick="javascript:openPage('review.html?review=I am a title')"/>

Explanation: Use of AVT (Attribute Value Templates).

like image 23
Dimitre Novatchev Avatar answered Sep 22 '22 23:09

Dimitre Novatchev