ePub and Javascript

2020-06-27 02:42发布

I'm using a MacBook Air and have been looking into ePub creation. I've taken a copy of the epub3 boilerplate from github click here. I have in a javascript file added the following

var txt = document.createTextNode(" This text was added to the DIV.");
document.getElementById('contenty').appendChild(txt); 

<script src='../js/main.js'></script>

When I open the compiled epub in iBooks it doesn't show This text was added to the DIV. in the div #contenty.

Using Kitabu it actually works but is there a way to get Javascript to work in iBooks?

Thanks

1条回答
太酷不给撩
2楼-- · 2020-06-27 03:19

Javascript will most definitely work in iBooks: just make sure to include your JS files in the opf manifest as items:

<item id="[someUniqueID]" href="[fileLocation/fileName.js]" media-type="text/javascript"/>

You also need to declare the page(s) that reference the script file as scripted in the OPF:

<item id="[pageID]" href="[pageLocaiton.xhtml]" media-type="application/xhtml+xml"  properties="scripted" />

Note: you can get sideloading to work even without the above fixes, but you won't be able to actually put the book in the iBookstore without them.

Right now, your JS code won't work: because it is executing before the page loads: try doing a <body onload="init()"> and wrap your second line of code (document.getElementById....) in an function init(){}.

Best of luck any happy Scripting!

查看更多
登录 后发表回答