检测localStorage的任何变化? [英] Detect any change in localStorage?

查看:167
本文介绍了检测localStorage的任何变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测HTML5 localStorage中的任何更改,然后如果确实有任何更改,则调用某些函数?
我将某些密钥存储为名称为e1,e2,e3等等......

Is there any way to detect any change in HTML5 localStorage and then call certain functions if there is any change indeed? I have certain keys stored with names as "e1", "e2", "e3", and so on...

我想检测是否删除或添加任何键,然后在有任何更改时触发某些功能...

i want to detect if any key is removed or added and then trigger some functions if there is any change...

推荐答案

根据规格存储界面发出 存储事件它影响的全局对象。

According to specs Storage interface emits storage event on global objects which it affects.

所以在你的情况下你可以只添加处理程序

So in your case you may just add handler

window.addEventListener("storage", function () {
    // do your checks to detect
    // changes in "e1", "e2" & "e3" here
}, false);

甚至不需要 jQuery

这篇关于检测localStorage的任何变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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