最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5JS storage event handler - Stack Overflow

programmeradmin0浏览0评论

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

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";
    }
}
Share Improve this question edited Oct 4, 2020 at 12:10 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Jun 16, 2010 at 15:57 KenKen 2,7344 gold badges26 silver badges29 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 19

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

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.

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

To communicate between multiple tabs we can use storage event. You store a data item in local storage and you will get to know that your local storage is updated.

Check out the code.

      window.addEventListener("storage", storageEventHandler, false);

      function storageEventHandler(evt) {
        alert("storage updated");
      }

      function clickme() {
        localStorage.setItem("someKey", "someValue");
      }
<button onclick="clickme()">Click Me</button>

Note: storage event only fires in other tabs. Not inside the tab, you are in currently. click^ :)

发布评论

评论列表(0)

  1. 暂无评论