DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

using xslt to pass node value as an argument to a javascript function

StackOverflow https://stackoverflow.com/questions/13216758

Question

I am new to this so I don't know whether this approach is the correct way to pass an argument. Please help me correct this or suggest another way to do this. I want to pass a node value from the xslt to a javascript function.

This is my XML file :

   <?xml version="1.0"?>
   <?xml-stylesheet type="text/xsl" href="sample.xsl"?>
   <One>
       <Two>
           HelloWorld
       </Two>
   </One>

This is the xslt file :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:variable name="url" select="One/Two"/>
<xsl:template match="/">
<html>
    <script>
        function Myclick(vara)
        {
            alert(vara);            
        }
    </script>
    <xsl:for-each select="One">
        <a>
            <xsl:attribute name="href">
                http://www.google.com
            </xsl:attribute>
            <xsl:attribute name="onClick">                      
                alert($url);
                Myclick($url);
            </xsl:attribute>            
            <xsl:value-of select="Two"/>
        </a>
    </xsl:for-each>
</html>
</xsl:template>

Solution 2

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
    <xsl:variable name="url" select="One/Two"/>
    <xsl:template match="/">
        <html>
            <head>
                <script>
                    function Myclick(a)
                    {
                        var href = a.getAttribute('href');
                        alert(href);
                        location.href= href;
                    }
                </script>
            </head>
            <body>
                <xsl:for-each select="One">
                    <a>
                        <xsl:attribute name="href">
                            http://www.google.com
                        </xsl:attribute>
                        <xsl:attribute name="onClick">
                            javascript:Myclick(this); return false;
                        </xsl:attribute>
                        <xsl:value-of select="Two"/>
                    </a>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

OTHER TIPS

Your XSLT code is not calling any Javascript function; it is merely generating HTML.

Do you know what HTML you want to generate? If so, tell us, and we can help you generate it. If not, you shouldn't be trying to write XSLT code - never start writing a program (in any language) until you know what output you want it to produce.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow