Is it possible to handle a parameter to a javascript function in XSLT where the script is between tags <script type=text/javascript>....</script>.
If possible could someone give a example. Thanks.
问题:
回答1:
I think you're getting confused between the two things: XSLT is something that is used (in this case) to generate HTML/javascript - once that generated HTML/javascript is received by the browser then the javascript can be run. I'm unaware of any such concept of the XSLT "passing" a variable to a javascript function.
My guess is you want something like this XSLT...
<script type="text/javascript">
var myVar = "<xsl:value-of select="XPATHVALUE"/>";
<xsl:text disable-output-escaping="yes"><![CDATA[
function myFunc(){
alert(myVar);
}
]]></xsl:text>
</script>
If you put the main "body" of the javascript within the xsl:text element, it means you won't get caught out using reserved characters (such as < > etc).
The generated HTML/javascript that is set to the browser would end up as something like this, meaning calling myFunc would display "hello world"...
<script type="text/javascript">
var myVar = "hello world";
function myFunc(){
alert(myVar);
}
</script>
Update
As MichaelKey has highlighted, the <xsl:text> element above is unnecessary. This should produce the same thing...
<script type="text/javascript">
var myVar = "<xsl:value-of select="XPATHVALUE"/>";
<![CDATA[
function myFunc(){
alert(myVar);
}
]]>
</script>