How to subscribe on localStorage but not on sessio

2019-07-14 05:38发布

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.

1条回答
姐就是有狂的资本
2楼-- · 2019-07-14 06:31

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);
查看更多
登录 后发表回答