How to subscribe on localStorage but not on sessionStorage events

青春壹個敷衍的年華 提交于 2020-01-05 03:24:30

问题


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:


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);


来源:https://stackoverflow.com/questions/32965288/how-to-subscribe-on-localstorage-but-not-on-sessionstorage-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!