Reputation: 133
This has been asked before, but I want to evaluate if this is possible?...
Is there a simple way to pass javascript variable into an xsl variable? Reason being, the variable will be coming from an external script.
Here's my non-working code...
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<div>
<script type="text/javascript">
var key = window.location.href;
</script>
<xsl:variable name="jsvar">$key</xsl:variable>
<xsl:value-of select="$jsvar"/>
</div>
</xsl:template>
I want to display "Term1" on the web page.
Any idea?
Upvotes: 1
Views: 7523
Reputation:
The script
tag is being output by the XSL, it's not something that defines anything that's executable in the context of the XSL. What you can do is define the XSL variable at global scope and re-use it both in the script and the div:
<xsl:variable name="jsvar">Term1</xsl:variable>
<xsl:template match="/">
<script>
var key = "<xsl:value-of select="$jsvar"/>";
</script>
<div>
<xsl:value-of select="$jsvar"/>
</div>
</xsl:template>
If you want a variable known to JS when JS is executing and do something with it like display it in an element in the browser page, then you have to do that in the JS:
<xsl:template match="/">
<script>
window.onload = function() {
document.getElementById('location').textContent = window.location.href;
};
</script>
<div id="location"></div>
</xsl:template>
Upvotes: 1