如何将事件绑定到sessionStorage? [英] How do I bind event to sessionStorage?

查看:254
本文介绍了如何将事件绑定到sessionStorage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以成功地将更改的事件绑定到localStorage(使用jquery):

I can successfully bind an event for a change to localStorage (using jquery):

$(window).bind('storage', function(e)
{
    alert('change');
});

localStorage.setItem('someItem', 'someValue');

如果我使用sessionStorage,该事件将不会触发:

If I use sessionStorage, the event will NOT fire:

 $(window).bind('storage', function(e)
{
    alert('change');
});

sessionStorage.setItem('someItem', 'someValue');

为什么是这样?

推荐答案

这是我想的方式。从 spec (加重点):

That is the way it's meant to be I think. From the spec (emphasis added):


setItem() removeItem() clear()方法在与
存储对象x中调用会话存储区域,如果
方法执行某些操作,那么在每个Document对象的Window
对象的sessionStorage属性的Storage对象与
相关联的相同存储区域中的 strong>,必须触发一个存储事件

When the setItem(), removeItem(), and clear() methods are called on a Storage object x that is associated with a session storage area, if the methods did something, then in every Document object whose Window object's sessionStorage attribute's Storage object is associated with the same storage area, other than x, a storage event must be fired

我认为这意味着事件将是在任何其他共享会话存储对象的文档中触发,而不是导致事件触发的文档。

I think what that means is that the event will be fired in any other document sharing the session storage object, but not the document that caused the event to fire.

更新

这是另一个非常相似的问题,这似乎与上面提到的一致(答案引用了规范的同一段)。

Here's another very similar question which seems to agree with what I've mentioned above (the answer quotes the same paragraph from the spec).

这篇关于如何将事件绑定到sessionStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆