HTML5 / JS storage event handler

24,979

Solution 1

Storage event handlers only fire if the storage event is triggered from another window.

How can I get an event to fire every time localStorage is updated in Safari 5+?

Solution 2

the 'storage' event occurred by the other tab in the browser. When you change the storage in one page and addEventLister also in this page , the window can not catch the message.

for example

You have two page, pageOne change the storage , pageTwo will catch the 'storage' message and handle this, but pageOne couldn't catch the message.

Share:
24,979
Ken
Author by

Ken

CPL (A) Pilot Working as full time Web Developer (Mostly PHP). Also freelance developer in Java & PHP

Updated on October 05, 2020

Comments

  • Ken
    Ken over 3 years

    I'm using Safari webkit's engine together with HTML5 and JS to create an offline application now I'm using the sessionStorage array to store status of my application(simulation).

    The storage data works fine with the inspector the functions work fine it's the event handler that isn't responding.

    The test preformd by Anurag at http://jsfiddle.net/pvRgH/ also doesn't work here:

    window.addEventListener('storage', storageEventHandler, false);
    function storageEventHandler(evt){
        alert("storage event called key: " + evt.key );
        switch(evt.key){
            case 'bat1':
            case 'bat2': batteryDCMeter(); break;
            case 'extPowerOn': rechargeBattery(); break;
        }   
    }
    
    function load()
    {
        dashcode.setupParts();
        //set HTML 5 key/value's
        sessionStorage.setItem('bat1', 'OFF');
        sessionStorage.setItem('bat2', 'OFF');
        sessionStorage.setItem('bat1DC', '26.2');
        sessionStorage.setItem('bat2DC', '26.2');
    }
    
    function bat1OnOff(event)
    {
        if(sessionStorage['bat1'] == 'OFF'){
            sessionStorage['bat1'] = 'ON';
        }else{
            sessionStorage['bat1'] = "OFF";
        }
    }
    
    
    function bat2OnOff(event)
    {
        if(sessionStorage['bat2'] == 'OFF'){
            sessionStorage['bat2'] = 'ON';
        }else{
            sessionStorage['bat2'] = "OFF";
        }
    }
    
  • Ken
    Ken almost 14 years
    The link you provided the code doesn't give any results. Check updated question for more code
  • Chance
    Chance almost 14 years
    So there's no alert popping up indicating the storage event handler was called for you? What version of Safari are you testing this on?
  • Ken
    Ken almost 14 years
    No nothing is happening I'm running Safari 5.0(6533.16) MacOSX 10.6.3
  • Stefano
    Stefano about 13 years
    Nothing happens for me either (Safari 5.0.3, Chrome 10)