简体   繁体   中英

Embed Javascript file in an XSL File

I have an xsl file, that generates a link "Preview".

  <a>
       <xsl:attribute name="href">
           <xsl:text>#preview</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="onclick">
           <xsl:text>embeddedPreview("</xsl:text>
           <xsl:text>http://docs.google.com/viewer?url=</xsl:text>
           <!--Google can't reach a local development machines, so you may want to code in your production server, where Google downloads the file from-->
           <!--<xsl:text>http://example.edu/</xsl:text>-->
           <!-- Getting bitstreams by ID  XMLUI: /xmlui/bitstream/id  JSPUI: /jspui/retrieve -->
           <xsl:text>/xmlui/bitstream/id/</xsl:text>       
           <xsl:value-of select="substring(@ID,6)"/>
           <xsl:text>&amp;embedded=true</xsl:text>
           <xsl:text>");</xsl:text>
       /xsl:attribute>
           Preview
   </a>

Whenever the link is click, the onclick event calls a javascript function called embeddedPreview. embeddedPreview function is stored in mylibrary.js:

/**
 * Creates an iframe with the specified source
 * @REQUIRES jQuery
 * @REQUIRES that there exists an element with ID preview-embed
 */
function embeddedPreview(source) {
    if($("#embed").length > 0) {
        //set the source to the location asked for
        $("#embed").attr("http://19.46.1.34:8081/xmlui/handle/123456789", source);
    } else {
        //Create the embed iframe
        $("#preview-embed").append("<iframe id='embed' src='"+source+"' width='100%' height='342px' style='border:none;'/>"); //requires jQuery
    }
}

Whenever I click the Preview link, an error occurs saying:

UnCaughtReferenceError: embeddedPreview is not defined
onclick

How to embed Javascript in an XSL file?

The problem is not in the XSLT, because the error given specify that embeddedPreview was called, but not found (not defined) which mean that the XSLT did the correct conversion.

So the possibilities are :

  • There might be a javascript error causing the compilation process to abort before the function definition
  • You forgot to put the function definition in a tag

Make sure you check your javascript error in the console, and comment if this does not solve your issue or if you find any other errors.

Also if this does not fix it, it would help if you could post the converted link ( tag) after the conversion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM