Using Custom JavaScript

You can include a single JavaScript file in every generated HTML file in the EPUB using the epub.javascript.file Ant parameter. You can specify the directory within the EPUB package to put the JavaScript file in using the epub.javascript.dir Ant parameter.

If you need to include more than one JavaScript file then you must using the graphic map extension point to include the additional files in the EPUB package and have them properly listed in the EPUB manifest. See Graphic Map Generation for guidance. The XSLT module map2epubIncludeJavaScript.xsl demonstrates how to extend the graphic map processing and the base JavaScript reference generation processing.

If you need multiple JavaScript files it is probably better to use JavaScript management tools to produce a single JavaScript file that is optimized for size and performance.

Note that JavaScript support varies widely in EPUB readers. EPUB3 clarifies the requirements for JavaScript support but does not require support for JavaScript. As of September 2016 both iBooks and Adobe Digital Editions seem to have pretty reliable JavaScript support.