|  06-27-2021, 09:19 AM | #1 | 
| Member  Posts: 22 Karma: 10 Join Date: Jun 2021 Device: Any ereader for Android or iOS | 
				
				How to load javascript module in epub xhtml
			 
			
			I have an ebook (ebook1.epub) that loads a javascript file (script1.js) like so: Code: cat EPUB/xhtml/raw/ch1.xhtml
<!--?xml version='1.0' encoding='UTF-8'?-->
<!DOCTYPE html><html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:xlink="http://www.w3.org/1999/xlink">
    <head>
        <script type="text/javascript" src="../../js/script1.js"></script>
...
    </head>
...
</html>
# --------------------------------------------------------------
cat EPUB/package.opf
...
<item id="js4" href="js/script1.js" media-type="text/javascript"/>Code: calibre ebook1.epub ------- Now, I need to use Javascript module file (ES6) (script2.js) The javascript module file is loaded like so: Code: cat EPUB/xhtml/raw/ch1.xhtml
<!--?xml version='1.0' encoding='UTF-8'?-->
<!DOCTYPE html><html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:xlink="http://www.w3.org/1999/xlink">
    <head>
        <script type="module" src="../../js/script2.js"></script>
...
    </head>
...
</html>
# --------------------------------------------------------------
cat EPUB/package.opf
...
<item id="js4" href="js/script2.js" media-type="text/javascript"/>
```Code: calibre ebook2.epub Calling function from script2: - does not print anything to the terminal. - the terminal shows an error message: Code: ERROR: clbr://internal.sandbox/book/EPUB/js/script2.js:0: Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec. Thanks, Avner | 
|   |   | 
|  06-27-2021, 12:07 PM | #2 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			what version of calibre, what OS? As far as I know, calibre does send mimetypes for js files. It's likely a bug in Qt WebEngine's custom URL scheme handling.
		 | 
|   |   | 
| Advert | |
|  | 
|  06-27-2021, 12:13 PM | #3 | 
| creator of calibre            Posts: 45,604 Karma: 28548974 Join Date: Oct 2006 Location: Mumbai, India Device: Various | 
			
			And I just tried it on the current calibre release, it works fine.
		 | 
|   |   | 
|  06-27-2021, 04:09 PM | #4 | 
| Member  Posts: 22 Karma: 10 Join Date: Jun 2021 Device: Any ereader for Android or iOS | 
			
			@kovidgoyal, thanks for replying The Calibre version is 4.99.4. The OS is Ubuntu 20.04 Can you share a LINK TO Nn example with a call to javascript module? Maybe I'm doing something incorrectly. Thanks, Avner Code: calibre --version calibre (calibre 4.99.4) lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.2 LTS Release: 20.04 Codename: focal | 
|   |   | 
|  06-27-2021, 06:13 PM | #5 | |
| null operator (he/him)            Posts: 22,010 Karma: 30277294 Join Date: Mar 2012 Location: Sydney Australia Device: none | Quote: 
 Suggest you download and install the latest version from the calibre site ==>> https://calibre-ebook.com/ BR | |
|   |   | 
| Advert | |
|  | 
|  06-27-2021, 08:12 PM | #6 | 
| Member  Posts: 22 Karma: 10 Join Date: Jun 2021 Device: Any ereader for Android or iOS | 
			
			I installed the latest Calibre version (5.22.1) from here After install, I was able to load module javascript files, and see messages from console.log() in the terminal where I launched Calibre from. Code: cat EPUB/xhtml/raw/ch1.xhtml
<!--?xml version='1.0' encoding='UTF-8'?-->
<!DOCTYPE html><html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:xlink="http://www.w3.org/1999/xlink">
    <head>
        <script type="module" src="../../js/script2.js"></script>
...
    </head>
...
</html>
# --------------------------------------------------------------
cat EPUB/package.opf
...
        <item id="js4" href="js/script2.js" media-type="module"/>When I ran calibre with Code: calibre ebook2.epub I can see messages from Console.log() in the terminal. kovidgoyal, thanks for your help. Avner | 
|   |   | 
|  | 
| 
 | 
|  Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post | 
| Javascript in ePub. | Vijay | ePub | 22 | 12-28-2023 09:43 AM | 
| Aura {A1} epub 3.0 renderer javascript support? | pluma | Kobo Reader | 1 | 01-13-2017 03:53 PM | 
| Hints for javascript in epub ? | fxp33 | Kobo Developer's Corner | 6 | 08-09-2014 01:18 PM | 
| Javascript in ePub | Techno4 | ePub | 9 | 11-05-2012 12:50 PM | 
| epub et javascript | zelda_pinwheel | Software | 9 | 12-09-2010 04:56 PM |