View Full Version : Javascript in ePub.


Vijay
11-20-2010, 12:20 AM
How to embed javascript in ePub, anybody having samples or any resource.
:help:

Jellby
11-20-2010, 06:12 AM
Javascript is not supported by the ePUB spec, nor by most ePUB readers.

Vijay
11-22-2010, 12:35 AM
Javascript is not supported by the ePUB spec, nor by most ePUB readers.

so its not feasible to make animation or playing media in a ePub??

wannabee
11-22-2010, 01:51 AM
Probably not what you want to do. Try this (http://www.pigsgourdsandwikis.com/2010/10/first-fiction-ebook-in-ibookstore-with.html)

charleski
11-22-2010, 07:49 AM
As Jellby said, the current standard doesn't provide for embedded media or scripting. There are a couple of non-standard solutions, though they should only be used if you are only targeting a specific reading device.

Here's (http://blog.threepress.org/2010/06/24/javascript-and-interactivity-in-ibooks/) Liza Daly's post on using JavaScript with iBooks. As with the other link, this will only work on the iPad.

Adobe's reading system will recognise flash elements, there's an example here (http://blog.threepress.org/2009/11/14/using-flash-video-in-epub/). This won't work on the majority of device implementations, but does work on the desktop reader.

jharker
11-22-2010, 09:30 AM
Liza Daly's example (linked by charleski above) is a great place to start.

Two important things to note: Inline scripts do not work in iBooks. You must write your script in a separate file and embed it using <script type="text/javascript" src="script.js"></script> Scripts aren't allowed to access remote files.
Also, I think I recall that you're limited to embedding ONE javascript file in your html. But I could be wrong about that.

whbenson
11-22-2010, 08:57 PM
FWIW, the following sample code works in both Sigil and iBooks.

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
//<![CDATA[
alert("screen width=" + screen.width);
//]]>
</script>
</head>

<body>
</body>
</html>


Also, a workaround to editing javascript files in Sigil is to give the file a .css extension and put it in the Styles folder. Both iBooks and Sigil are happy with this.
-Bill

Vijay
11-24-2010, 03:20 PM
i tried with monocle reader, its too supporting javascript.
@ Bill. your example is good, i tried with that its working. thnks..

killercatfish
09-27-2011, 10:26 PM
Hello!

I have watched liza's video and am pumped about the prospects for education. Any advice on where to read up/find examples on actually putting together an ePub with javascript and interactivity?

Thanks!

wannabee
09-27-2011, 11:31 PM
G'gay Killa' welcome to the forums.
You could try Liz Castro's (http://www.pigsgourdsandwikis.com/2011/06/javascript-accepted-in-ibookstore.html) comments.
Be aware that adding Java to an ePub will limit your audience. I don't think many reading devices will support it.

gus_
11-07-2011, 07:46 AM
bill, i have tried the exact code you posted, and it crashes sigil.

the alert window pops up twice, then a fatal error occurs.

i am using windows 7 64 bit and sigil 0.4.2

any ideas what may cause this behavior?

Toxaris
11-07-2011, 08:20 AM
Could be anything. Javascript is not supported as far as I know and it is still not in the specifications of ePUB. So it is kind of hit and miss.

whbenson
11-07-2011, 11:35 AM
bill, i have tried the exact code you posted, and it crashes sigil.

the alert window pops up twice, then a fatal error occurs.

i am using windows 7 64 bit and sigil 0.4.2

any ideas what may cause this behavior?
gus - you're right (though I think it worked before). In any case this was just to demonstrate inline javascript was ok. More typically you'd put functions in the header and call them from the text. The sample below works in sigil 0.4.2, at least on the Mac. You could also invoke this from a link with the href attribute. <a href="javascript:hitme()">hit me</a>


<head>
<title></title>
<script type="text/javascript">
//<![CDATA[
function hitme() {
alert("screen width=" + screen.width);
}
//]]>
</script>

</head>

<body>
<p onclick="hitme()">hit me</p>
</body>
</html>

Jim Lester
11-08-2011, 10:41 AM
Could be anything. Javascript is not supported as far as I know and it is still not in the specifications of ePUB. So it is kind of hit and miss.

Javascript is allowed in EPUB3 documents, but not EPUB2 documents. Any Reader that is not using WebKit (ie Adobe RMSDK) will only handle EPUB2 documents, and that's the vast overwhelming majority right now. As mentioned above, iBooks is the closest to an EPUB3 reader that is out there right now.

whbenson
11-08-2011, 02:30 PM
Javascript is allowed in EPUB3 documents, but not EPUB2 documents. Any Reader that is not using WebKit (ie Adobe RMSDK) will only handle EPUB2 documents, and that's the vast overwhelming majority right now. As mentioned above, iBooks is the closest to an EPUB3 reader that is out there right now.

Just for the record, one other webkit based reader with Javascript is the Kobo app for iOS.

Toxaris
11-08-2011, 02:45 PM
For the record, ePUB 3 is still not agreed upon.

pholy
11-09-2011, 12:51 AM
It was just approved last month:
EPUB 3.0 is the current revision of the EPUB standard. EPUB 3 was developed pursuant to a charter approved by the IDPF Membership in May, 2010. and was approved by the IDPF membership as a final Recommended Specification effective October 11, 2011.
And one company announced a reader (http://www.teleread.com/paul-biba/access-announces-new-epub-3-ereader-software-will-display-japanese-language/) already

Toxaris
11-09-2011, 06:21 AM
I stand corrected. I hope the biggest players will ignore the javascript and audio/video stupidity and first fix the problems with regards to READING, but I am afraid they dont.

DaleDe
11-09-2011, 05:30 PM
Just for the record, one other webkit based reader with Javascript is the Kobo app for iOS.
Another ePub 3 reader is the AZARDI 6.

Freeshadow
11-09-2011, 10:17 PM
I stand corrected. I hope the biggest players will ignore the javascript and audio/video stupidity and first fix the problems with regards to READING, but I am afraid they dont.
the question is how to get the fact that e-reading devices are (still?) bought by people who want to read in some heads.