如何订阅localStorage

如何订阅localStorage

本文介绍了如何订阅localStorage,但不能在sessionStorage事件中订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知:

  window.addEventListener('storage',function(event){
...
},false);

是在localStorage和sessionStorage事件上进行订阅。
只能订阅localStorage事件?



谢谢。

解决方案>

我不认为你可以,因为你说存储是。您只需在收到事件时检查事件的 storageArea 属性,并忽略会话存储中的那些属性。例如:

  window.addEventListener('storage',function(event){
if(event.storageArea == = localStorage){
//本地存储
}
},false);


As far as I understand:

window.addEventListener('storage', function(event){
       ...
}, false);

is subscription on both localStorage and sessionStorage events.Can I subscribe on localStorage events only?

Thanks.

解决方案

I don't think you can, as you say storage is fired on the window when any storage item changes. You just have to check the storageArea property of the event when you receive it, and ignore the ones from session storage. E.g.:

window.addEventListener('storage', function(event){
    if (event.storageArea === localStorage) {
        // It's local storage
    }
}, false);

这篇关于如何订阅localStorage,但不能在sessionStorage事件中订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:34