Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Hastings, East Sussex
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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);
    }

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Make sure your <script> is included in the produced template. i.e.:

    Code:
    <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>

  • #3
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,460
    Thanks
    1
    Thanked 23 Times in 21 Posts
    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!
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #4
    Regular Coder
    Join Date
    May 2002
    Location
    Maine, USA
    Posts
    573
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    javascript&#58
    W/o colon coded:
    Code:
    javascript:
    boxer_1
    CodingForums Moderator
    "How did a fool and his money get together in the first place?"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •