How to send a Javascript call from Page to Menu?
Hi,
Is there a way to launch a Javascript function on the Menu (index.html) from a Page? Basically, I have a JS on the index.html to play background music but I want to be able to start/stop the music from one of the book pages.
Also, is there a way to create a JS button in one of the Pages that can open the Menu (since double-tap may not be obvious to some users)
I would appreciate any help on this.
Thank you,
Jorge
It's not possible right now from pure JavaScript, you'd have to implement some ObjectiveC bridge. But that's a good idea. :) I've added this as a feature. :)
Thank you :)
Unfortunately, I don't know any ObjectiveC :/ but I'll see if I can make sense of the code somehow.
Thanks for making this product :)
Could I confirm, are you saying its not possible to link show of the table of contents to an object on the page using javascript? Ive been looking at the same question for some time now too and havent found a solution which really works. Is it not possible to show the TOC with javascript?
Appreciate your thoughts
Nope, it's a feature not present in Baker. Any update will be posted on this issue. ;)
You could use the localStorage event to call a function:
// Code in index.html
window.addEventListener('storage', function(e) {
var func = localStorage.getItem('index_function');
if (func) {
callFunction(func);
// must remove it so it isn't called on all
// subsequent localStorage calls
localStorage.removeItem('index_function');
}
}, false);
// Code in other pages
var selectors = document.querySelectorAll('a[data-index]');
foreach(var i in selectors) {
if (selectors.hasOwnProperty(i)) {
// Set localStorage to the value of the data-index attribute.
// This will call the storage event on all other pages in the book
// but will only be captured by the index.html code (above)
localStorage.setItem('index_function', selectors[i].dataset.index);
}
}
/*
<a href="#" data-index="foo">Execute foo</a>
<a href="#" data-index="bar">Execute bar</a>
*/
The code is untested and hastily written but demonstrates how you can work around this issue.
It won't display the index frame if its hidden, but the function will execute.
Thank you so much for this, al3d. This really helps! I already did some quick tests and it does allow me to communicate between the pages and the index.
Hi, I need to do what ayarmanqu says:-" is there a way to create a JS button in one of the Pages that can open the Menu (since double-tap may not be obvious to some users)"
He also says that "it does allow me to communicate between the pages and the index", but i've tried with localstorage.. but as folleto says...the index and the pages can't communicate by pure javascript. The value of the localstorage var in the index.html is never updated although i use a setitem in the load of other pages to assign a different value.
Here ayarmanqu says that it is possible to read a localstorage var from the index and give a different value from other pages with his code..a setinterval that would check the value of the localStorage variable every second.I've tried this but the result is the same..no updates for the localstorage var when i check from the index.html. https://github.com/Simbul/baker/issues/227#issuecomment-3520768 there's a user that has implemented bookmarks but with Web SQL Database and its said not to use becouse-" is officially deprecated by the W3C."This could be a way to do that connection. Please ayarmanqu ..could you tell me how you resolved this issue??I would really appreciate your help.
thanks!!
I answer myself about the previous question...it allows me to communicate between the index and the other pages with a localstorage var placed in a js file and called from the index.html with a setinterval!!!thanks!!! the other question is...how to create a JS button in the pages that can open the Menu as a dock as the user ayarmanqu did..that question is still unresolved..
I have tried to use the code above for my implementation of an app based on Baker, however I can't make it to work. I fixed all the obvious errors in code above, but I simply cannot make "window.addEventListener('storage'..." to work. It never catches any events. I can see that localStorage is modified (variables are being set), but no notification being fired.
I can also see in safari debugger that Baker (as stated in docs) fires 4 instances of WebUI (index and 3 normal "pages") so this problem shouldn't be caused by some already existing explanation which is: "Storage change event is only triggered by separate browsers/tabs". Can anyone help me out/suggest anything about it? Maybe someone could share some code?
It allows me to communicate between the index and the other pages with a localstorage var placed in a js file and called from the index.html with a setinterval,its very simple when you understand the way it works.
//code in a file called menu.js placed in the js folder.
var ourInterval = setInterval(check, 1000);
function check(){
//alert(localStorage.tipomenu);
switch(localStorage.tipomenu)
{
case '1':
document.getElementById("nav1").style.display='block';
document.getElementById("nav2").style.display='none';
break;
case '2':
document.getElementById("nav2").style.display='block';
document.getElementById("nav1").style.display='none';
break;}}
//code in the index.html file
//head tag
<script src="js/menu.js">
//body tag
<div class="navigation" id="nav1" >aaaa</div>
<div class="navigation" id="nav2" >bbbb</div>
Add the following code to the head tags of the magazine pages from which you want the var "tipomenu" to change the value(1 or 2 in my case)
(cover.html)
window.addEventListener("focus", function() {
localStorage.removeItem('tipomenu');
localStorage.tipomenu=1;
}, true);
//the focus function triggers when the page is showing.
(page1.html)
window.addEventListener("focus", function() {
localStorage.removeItem('tipomenu');
localStorage.tipomenu=2;
}, true);
with this code you'll achieve to have several div tags in the index.html and depending the page of the magazine you are loading ,the index.html would show one div or another so you can control the content shown in the menu displayed below. The code above is an example and must be improved, but i wish it could be helpfull and get u ideas.
Sorry for my english!! T__T
rokanne, thank you for your answer! However, your code has to check localStorage each and every second. I think it's a little bit too much - this should work based on event 'storage-has-changed'. Do you (or anyone else) can confirm that asking for localStorage every second is the way to go?
yah...i know..thats the only matter..i havent found a solution for that.
I made some pretty extensive testing and whatever I do with localStorage on index or other pages is not being picked up by other pages. Simply saying the event 'storage' is not being sent at all. I don't know if that's problem with Baker or UIWebView. There's no exception or warning, nothing. So in the end I went with interval in js solution. It's working really fast (average 0ms :) ), but still I feel it's hackish. If anyone will find a way to use native event, please let me know how (here or msg). Thanks!
I think that until we - or someone else with us - implements the communication bridge it's going to stay a bit hacky unfortunately. :)
Ok. Can you confirm that it's a problem with UIWebView ?
I didn't test thoroughly that solution, so I can't really answer. The bridge will be a solution involving Objective-C code, not just javascript. :)
A question, was the original poster asking for how to implement the same type of menu that is in the Forgotten Colours book? I have wondered how it was possible and why the Forgotten Colours book or questions about its menu are not mentioned in the forums.
I have read all the posts with the word "menu" in it. If I was looking in the wrong place for this solution, could I please have a link to the correct thread? Thank you.
The menu in Forgotten Colours is a custom development. :)
@folletto any clues about how it was achieved or who to hire for such a custom development?
Well it's custom Objective-C code, you should ask them. The link is on our showcase. :)