HTML5 / JS storage event handler

◇◆丶佛笑我妖孽 提交于 2019-12-03 19:34:36

问题


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";
    }
}

回答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+?




回答2:


Could you provide some more code for how you are storing the keys? It works for me on Safari - http://jsfiddle.net/pvRgH/




回答3:


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.



来源:https://stackoverflow.com/questions/3055013/html5-js-storage-event-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!