javascript - Using localstorage checks in a different window -


i'm trying make basic webapp. it's puzzle appears on time, when find links or urls.

the puzzle has 8 pieces, , appear when visit hash. hashes setup using backbone.js, , each trigger function shows piece corresponds it.

the hashes go - "index.html#hide/one", "index.html#hide/two", "index.html#hide/eight".

every time hash triggered, shows piece using javascript function adds class element. easy enough, right?

the problem is, hashes open in different window. main window "index.html#hide". need create localstorage value each piece, check on main page, , if it's set "yes", execute function.

is possible? , if so, how go doing it?

thanks in advance,

-mitchyl

edit - here's source code if anyone's interested. i'm not quite sure what's relevant , what's not, here's code in it's entirety. http://pastebin.com/q4hpjtq8

rather polling localstorage main window, it's better use sort of direct communication between windows.

if windows same origin (same protocol, domain , port) , 1 window opens others, can directly call js functions 1 window long save window handle. windows main window opened, main window in window.opener call globally defined function in main window like:

window.opener.updatepuzzle(data); 

you can use window.postmessage() exchange data between windows seems (on surface) bit cleaner , isn't restrictive same origin (because requires 2 cooperating windows), there limitations postmessage() in ie before ie11 still make difficult rely on (thanks ie).


if main window did not open other windows in way (e.g. user typed other urls), windows cannot directly communicate 1 within browser. in case, they'd either have exchange data via server or poll data via localstorage.

here's pretty tutorial on localstorage: http://www.sitepoint.com/an-overview-of-the-web-storage-api/:

from 1 window:

 localstorage.setitem("hash4", true); 

from other window:

 setinterval(function() {      var maxhash = 5;      (var = 0; < maxhash; i++) {          var val = localstorage.getitem("hash" + i)          if (val) {              // hash value found true          }      }  }, 1000); 

as sounds you're doing on mobile, should know polling continuously not ideal on mobile bunch of reasons. first off, it's battery drain when it's allowed run. second off, mobile device not let run continuously (as tries save battery).


you bit more sophisticated how store , rather use n separate keys, store json in 1 key represented object contained values in 1 localstorage key.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -