JavaScript Code

Top Previous Topic Next Topic  Print this topic

Custom JavaScript code can be executed by declaring an annotation with the xf:javascript property and referencing it from the fo:root tag or from an xf:form-action tag.

 

This is illustrated in the following example:

 

<?xml version="1.0" encoding="utf-8" ?>

<fo:root xf:annotation-reference="6" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xf="http://www.ecrion.com/xf/1.0">

       <xf:annotation id="6">

               <xf:javascript output-type="pdf">

                       app.alert("This is a JavaScript alert.");

               </xf:javascript>

               <xf:javascript output-type="html">

                       alert("This is a JavaScript alert.");

               </xf:javascript>

       </xf:annotation>

       <fo:layout-master-set>

               <fo:simple-page-master master-name="all-pages" page-width="8.5in" page-height="11in">

                       <fo:region-body region-name="xsl-region-body" margin="0.7in" column-gap="0.25in" padding="6pt" />

                       <fo:region-before region-name="xsl-region-before" extent="0.7in" display-align="after" padding="6pt" />

                       <fo:region-after region-name="xsl-region-after" extent="0.7in" display-align="before" padding="6pt" />

                       <fo:region-start region-name="xsl-region-start" extent="0.7in"/>

                       <fo:region-end region-name="xsl-region-end" extent="0.7in"/>

               </fo:simple-page-master>

               <fo:page-sequence-master master-name="default-sequence">

                       <fo:repeatable-page-master-reference master-reference="all-pages" />

               </fo:page-sequence-master>

       </fo:layout-master-set>

       <fo:page-sequence master-reference="default-sequence">

               <fo:flow flow-name="xsl-region-body" font-size="12pt" font-family="Times">

                       <!-- this will have no effect, javascript annotation must be referenced from fo:root or xf:form-action -->

                       <fo:block font-family="Times" xf:annotation-reference="6">Hello World</fo:block>

               </fo:flow>

       </fo:page-sequence>

</fo:root>

 

The JavaScript code will be executed each time the document is opened.

 

Note: The javascript will NOT be executed automatically if the annotation is referenced from another element than fo:root. If it's referenced from an xf:form-action element, then it will be executed when the event associated with the action is raised. See more in the PDF Forms topic.

 

Because there are differences between JavaScript syntax across output formats, you can optionally specify the output-type attribute to export a certain part of JavaScript code to only a certain output format.

 

The possible values for output-type are all (default), pdf and html.

 

For example, if there is an xf:javascript element with output-type set to pdf and one with no output