Monday, 9 September 2019

Local Storage is not shared between pages

I have an epub3 book with 2 pages as well as a Table of Contents Page. I am viewing this book in Apple's Books, their inbuilt epub3 reader, on Mac OSX. The two pages appear side by side. The first page is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=500, height=600"/>
    </head>
    <body>

<p id="result"></p>

<script>
//<![CDATA[
  var current_page = "1";
  var other_page = "2";

  var t = 0;

  setInterval(function() {

    var d = new Date();
    var storage = localStorage; 

    storage.setItem("t"+ current_page, d.toLocaleString());

    document.getElementById("result").innerHTML = storage.getItem("t"+ current_page) +" "+storage.getItem("t"+ other_page);    
  }, 1000);

//]]>
</script>

    </body>
</html>

and the only thing different in my second page is:

  var current_page = "2";
  var other_page = "1";

So every second, Page 1 saves the current time to Local Storage as t1, and Page 2 does the same for the value t2. At the same time, both pages are reading both t1 and t2 from Local Storage, before their values are displayed to screen. However in ibooks, Page 1 only manages to display the current value for t2 when the page is reloaded - like when I flip to the Table of Contents and then back to Page 1 and 2 again. With something similar happening for Page 2 with regard to t1.

So at time 21:10:00, Page 1 might display:

08/09/19, 21:09:18 08/09/19, 21:08:58

and Page 2:

08/09/19, 21:09:22 08/09/19, 21:08:01

I also tried using Session Data but Page 1 can't ever read t2 and Page 2 can't read t1. So, this would be displayed instead:

08/09/19, 21:09:18 null

I can think of several applications where it would be very useful for Pages to communicate with each other. For example, if a video is playing on one page, it would be useful to stop it if a video on another page is started. This would normally be sone using Session Storage.

Likewise, if the user is asked on Page 1 to enters the name of the main character of the story, then that entry should appear immediately on Page 2 once it is entered.

Is there any other way for Pages to communicate with each other in epub3 other than Local or Session Storage?



from Local Storage is not shared between pages

No comments:

Post a Comment