![]() |
#1 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 557
Karma: 400004
Join Date: Feb 2009
Device: ONYX M96
|
How to start javascript when a page is seen by user in iBooks
Hi,
I have an EPUB3 with several files with javascript scripts. One file has a javascript routine that show a little animation in page when the file (page) is opened. It works fine in ADE, but not in iBooks. The problem is that iBooks seems to "prepaginate" ebook in background, so it starts the javascript routine *before* the page is showed to user. So, when the user turns the page there is no animation because it started and ended during the iBooks prepagination process. Is there a way to start a javascript script *only* when the user turns the page and see the content in iBooks? Thank you for suggestions. Fabrizio |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 5,680
Karma: 23983815
Join Date: Dec 2010
Device: Kindle PW2
|
Since I don't how to do this in plain JavaScript, I usually embed jQuery and use the following function:
Code:
$(document).ready(function() { // your code }; If adding ready() doesn't do the trick, post an example epub3 file. |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
A Hairy Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 3,303
Karma: 20171571
Join Date: Dec 2012
Location: Charleston, SC today
Device: iPhone 15/11/X/6/iPad 1,2,Air & Air Pro/Surface Pro/Kindle PW & Fire
|
I'm certainly not a javascript expert either, but if ibooks is running said script before you flip to that page I would think it is because the "page" is run when the file (chapter) is opened. If you can't get Doitsu's method to work, perhaps putting that page of text and associated JS into its own file will do the trick?? It may look funny when the epub is reflowed - the previous page could cut off midway down the screen - but at least you know that JS won't run until that page is opened.
FWIW |
![]() |
![]() |
![]() |
#4 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 557
Karma: 400004
Join Date: Feb 2009
Device: ONYX M96
|
Quote:
(I used your example EPUB with some fast changes) f. |
|
![]() |
![]() |
![]() |
#5 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 557
Karma: 400004
Join Date: Feb 2009
Device: ONYX M96
|
Quote:
|
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 5,680
Karma: 23983815
Join Date: Dec 2010
Device: Kindle PW2
|
@fbrzvnrnd: I only have very limited JavaScript skills, but, IMHO, the code looks OK. This appears to be an iBooks bug feature.
The animation actually works with iBooks, but only once. (If you have a very fast Mac or iPad your might not even see it.) It looks like iBooks caches chapter html files and doesn't reload them when they're re-selected via the TOC. A workaround would be to disable the animation for iBooks apps. You can test for iBooks apps with the following JavaScript function: Code:
navigator.epubReadingSystem.name |
![]() |
![]() |
![]() |
#7 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 557
Karma: 400004
Join Date: Feb 2009
Device: ONYX M96
|
Quote:
I wait 60 sec. (for example). I flip page to chapter 3. The animation is gone. I can see the animation only if I flip fast to chapter 3, or if I close the ebook and reopen it in chapter 3. |
|
![]() |
![]() |
![]() |
#8 |
Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 629
Karma: 1623086
Join Date: Jan 2012
Device: iPad, iPhone, Nook Simple Touch
|
I suspect (but am not certain) that the solution is to watch for scroll events, check document.documentElement.scrollTop against the position of the element you care about, and then trigger your animation if the element is on the screen at the end of the scroll event.
Worth a try, anyway. |
![]() |
![]() |
![]() |
#9 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 557
Karma: 400004
Join Date: Feb 2009
Device: ONYX M96
|
Quote:
f. |
|
![]() |
![]() |
![]() |
#10 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 7,025
Karma: 90000009
Join Date: Nov 2011
Location: Charlottesville, VA
Device: Kindles
|
I don't have a solution to your problem and this is a bit off topic, but...
Amazon has samples of something they call "Kindle in Motion" which (among other things) supports animation that starts automatically when the user reaches its page. They haven't actually announced this feature yet and there is no information on the tools or markup required to create this effect. Just something to keep in mind in case you want to put your book on Amazon eventually. |
![]() |
![]() |
![]() |
#11 | |
Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 629
Karma: 1623086
Join Date: Jan 2012
Device: iPad, iPhone, Nook Simple Touch
|
Quote:
![]() |
|
![]() |
![]() |
![]() |
#12 |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 557
Karma: 400004
Join Date: Feb 2009
Device: ONYX M96
|
I do not think the scroll position could work: every chapter is a new xhtml file, so I got a different value (0) for each page.
I have the same problem when I use a script in a chapter that uses the localstorage. For example: chapter 1, after a user action, put a "yes" value in locastorage. Chapter 4, if the value in localstorage is "yes", must use red color for paragraph. This is working in ADE. But iBooks starts ALL the scripts of ALL the pages when load the ePub the first time. So no way to check the "yes" value in localstorage. |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Loading page per page option for iBooks? | momoha | ePub | 10 | 03-13-2015 01:25 AM |
iBooks New User: Can I access them on my Windows PC? | FatGuy | Apple Devices | 10 | 12-12-2014 09:34 AM |
Print user manual/quick start guide? | DennyP | Calibre | 6 | 03-14-2011 06:07 PM |
JavaScript and interactivity in iBooks | kjk | Apple Devices | 2 | 08-16-2010 10:14 AM |
Can this be done - remove javascript from a page | ScottMattes | Workshop | 1 | 05-17-2006 01:27 AM |