简体   繁体   English

如何订阅localStorage但不订阅sessionStorage事件

[英]How to subscribe on localStorage but not on sessionStorage events

As far as I understand: 据我所理解:

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

is subscription on both localStorage and sessionStorage events. 是对localStorage和sessionStorage事件的订阅。 Can I subscribe on localStorage events only? 我只能订阅localStorage事件吗?

Thanks. 谢谢。

I don't think you can, as you say storage is fired on the window when any storage item changes . 我不认为你可以,就像你说的storage窗口时的任何存储项目的变化上触发 You just have to check the storageArea property of the event when you receive it, and ignore the ones from session storage. 您只需要在收到事件时检查事件的storageArea属性,然后忽略会话存储中的那些属性即可。 Eg: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM