在同一窗口中使用localStorage监听更改 [英] Listen for changes with localStorage on the same window
本文介绍了在同一窗口中使用localStorage监听更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在同一页面上监听localStorage API中发生的更改(不在规格中说明的多个标签中)。
I want to listen for changes that are happening in the localStorage API on the same page (Not in multiple tabs like the spec says).
我目前正在使用此代码:
I am currently using this code:
var storageHandler = function () {
alert('storage event 1');
};
window.addEventListener("storage", storageHandler, false);
localStorage.setItem('foo', 'bar');
有没有人知道在一个页面上监听localStorage上的事件的vanilla JavaScript方式(没有jQuery)
Does anyone know a vanilla JavaScript way to listen to events on localStorage on one page (no jQuery)
推荐答案
由于JS是动态语言,只需重写原始函数。
Since JS is dynamical language just rewrite original functions.
var originalSetItem = localStorage.setItem;
localStorage.setItem = function(){
document.createEvent('Event').initEvent('itemInserted', true, true);
originalSetItem.apply(this, arguments);
}
这篇关于在同一窗口中使用localStorage监听更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文