在iBooks中打开页面时,间歇性地发生Javascript触发

问题描述:

我试图添加在我的电子书中播放音频的功能,并且用户可以暂停并在需要时启动此功能。在iBooks中打开页面时,间歇性地发生Javascript触发

我可以通过控制一个页面上的标准HTML5音频标签来做到这一点,但是当用户翻页时,我希望下一页能够记住这个状态,即音频是否暂停。

这个问题似乎是,当每个页面变成javascript有时会触发,有时不会。我已经尝试添加一个标准的监听

“document.addEventListener(‘DOMContentLoaded’,函数(事件){`

在头,但该页面转向时并不总是闪光。当我尝试在Chrome浏览器它完美

任何帮助,将不胜感激

编辑:这里是我试图运行代码:

document.addEventListener("DOMContentLoaded", function(event) { 
    do work 
    if (document.getElementById("audioreadalong") && localStorage.playaudio == "1"){ 
     if (localStorage.isPaused == "0"){ 
      document.getElementById("audioreadalong").play(); 
      if (document.getElementById("play") && document.getElementById("pause")){ 
       document.getElementById("play").setAttribute("style", "display: none;"); 
       document.getElementById("pause").setAttribute("style", "display: block;"); 
      } 
     } 
     else if (localStorage.isPaused == "1"){ 
      document.getElementById("audioreadalong").pause(); 
      if (document.getElementById("play") && document.getElementById("pause")){ 
       document.getElementById("play").setAttribute("style", "display: block;"); 
       document.getElementById("pause").setAttribute("style", "display: none;"); 
      } 
     } 
    } 
    else if(localStorage.playaudio == "0"){ 
     if (document.getElementById("play") && document.getElementById("pause")){ 
      document.getElementById("play").setAttribute("style", "display: none;"); 
      document.getElementById("pause").setAttribute("style", "display: none;"); 
     } 
    } 
}); 

这是在每个页面的头部引用我需要音频(奇数,正确的不是偶数左边的 - 每个双页是2个独立的网页)。

此代码在每次页面转动时都不运行。它在Chrome中每次都会重新加载页面。我无法在epub3规范中看到任何可以解释这一点的内容。

FINAL EDIT:我只能假设a)iBooks在页面上运行javascript的方式有一个错误;或者b)我有一些配置错误。我通过使用Apple对Read Along Books in their Asset Guide的内置支持解决了我的问题。我从iTunes下载了示例项目,现在使用SMILs以及iBooks名称空间和iBooks.js(位于示例项目中)。如果有人需要这个帖子的帮助,请参考这里。

感谢那些试图帮助或upvoted这一点。

+0

你是什么意思翻页? 您的第二页是在同一个DOM或另一个网页吗? – Rayon

+0

这是另一个网页 - 它是一个固定的布局风景书。 –

你将不得不存储音频的当前状态,在本地存储这样的:

localStorage.setItem("audioState", "true"); 

而且只用检索的localStorage的值:

localStorage.getItem("audioState"); 

并相应地实现条件...

+0

谢谢Rayon我现在正在这样做,但问题是,随着页面的转变,代码不会因为某种原因每次都运行。 –

+0

你能给我提供你的代码的任何基础结构吗? 您需要从“DOMContentLoaded”事件中的localstorage获取值。可以告诉我您从localstorage获得什么值? – Rayon