Web dev at the end of the world, from Hveragerði, Iceland

Localstorage & messaging in ePub

The short answer is: no.

The good news is that localStorage otherwise works in an ePub document in iBooks.

The idea I had was that, since we have to deal with the idiocy of having no CSS absolute positioning, I could use a document marked as “non-linear” in the spine as a kind of popup window. The theory being that you could click on a link or a button in the ebook, the non-linear document would open up full-screen, as it does in iBooks, with the UI/controls/wiggly widgets the reader could twiddle with, and, upon returning to the book, finding that the book had responded to those actions.

A classic example would be a gamebook: You get to a passage were you have to solve a task. All the links would be disabled until you click on the “fight” button (or “pick lock”, or whatever) and, once completed or failed, the book could decide which links to enable based on how it turned out.

Turns out that's not possible. Which is a shame, since, without overlays or some sort of popup, it is rather difficult to create usable interactivity widgets.

Localstorage works in iBooks, but it seems that the non-linear pages and the main book pages do not share a database. Values set in the non-linear page return null on regular book pages.

The second idea was to see if Cross Domain Messaging worked between non-linear pages and the opening book page. This would enable the non-linear page to send data to the opening page. It doesn't work. No matter what method you use to open the non-linear page.

If you want, you can have a look at the test ePub and see if I did something wrong.

You can also see the component xhtml pages: Localstorage Test. Messaging Test.

The localstorage test page is slightly different from the epub one, it has an added “target” attribute necessary to make the damn linked page pop up. Links with defined targets don't seem to work in iBooks.

Note: Neither of the tests will work in ePub readers that don't support javascript. To my knowledge that means only iBooks and possibly some of the browser-based readers.

ETA (July 2012): Apple turned off access to many of these APIs a while back. The lesson is that iBooks isn't a stable development environment.

You can also find me on Mastodon and Twitter