Helpful Information
 
 
Category: XML
Why won't the JS work when called from XSLT?

The first block of code below to run a function called 'Hi' and does not work.

The second block of code to run the JS alert function does.

For the first piece of code I can see that when I hover over the image it gives JavaScript:Hi('Server Function');

I cannot understand why one works and the other doesn't. The error I get is object expected. I've put the JS function below also, it's tiny and just for a test.

<xsl:if test="@attribute[. ='Server Function']">
<a>
<xsl:attribute name="href">
JavaScript:Hi('<xsl:value-of select = "@attribute"/>');
</xsl:attribute>
<img src="images/iconactionstart16.gif">
<xsl:attribute name="name">
ServerFunction
</xsl:attribute>
</img>
</a>
</xsl:if>

<xsl:if test="@attribute[. ='Machine Type']">
<a>
<xsl:attribute name="href">
javascript:alert();
</xsl:attribute>
<img src="images/iconactionstart16.gif">
<xsl:attribute name="name">
MachineType
</xsl:attribute>
</img>
</a>
</xsl:if>



function Hi(strNameOfImg)
{
alert(strNameOfImg);
}

Make sure your <script> is included in the produced template. i.e.:



<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>some title</title>
<script type="text/javascript">
//blabla
</script>
</head>
<body>
<xsl:apply-templates select="bla"/>
</body>
</html>
</xsl:template>

It might also help to either

(1) change the href to onclick and drop the "javascript:" part, or

(2) Make it "javascript:", not "java script:" (remove the space)

Cheers!

Originally posted by Alex Vincent
.....2) Make it "javascript:", not "java script:" (remove the space)

Just a little FYI; the forum automatically inserts the space in java^script: , especiallywhen it's inside the &#91;code][/code] tags, otherwise, sometimes it will add the space and sometimes it won't. The way to avoid it is to use ISO entities to code the colon (&amp;#58).
W/colon coded:

javascript&#58

W/o colon coded:

javascript:










privacy (GDPR)