We've been testing HTML widgets in iBooks 3 and had some success in connecting online. One of the drawbacks, though, is it requires user interaction to actually expand the widget and run Javascript. Is there a way to run Javascript in the background of the iBook, without requiring user interaction outside of regular paging?
For example, if we wanted to have Mixpanel running in the background, how would we do that? Is there another type of widget that allows this to happen? Or is there something else that runs javascript besides the HTML widget?
edit: To clarify, I'm using iBooks Author to create the full book, which has the functionality to add HTML widgets inside it.
I assume you are talking about a book authored using iBooks Author, which by the way would be a useful detail to put in your question. I don't know iBooks Author or its widget architecture or to what it extent it exposes the ability to place JS on a page.
However, in "normal" ebooks authored in (X)HTML/JS/CSS, there is no essential difference between JS in the reader and in a web browser.
I'm not sure what you mean by "running in the background"; JS runs when something tells it to, whether that something is it being placed on a page in a SCRIPT element, or put it in an onLoad or some other event handler.
Widgets in ibooks do not execute JS until they are activated by the user. So even though the link for the widget may be visible in your book, it's not capable of making connections via ajax or doing anything else until launched. Once launched, the widget has no access to the book content either. If you wanted to run mixpanel in a widget you could, but it wouldn't make sense unless your widget also contained all the content of the book (and a reading system).
来源:https://stackoverflow.com/questions/14326895/is-there-any-way-to-run-javascript-in-ibooks-without-user-interaction