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

How to subscribe on localStorage but not on sessionStorage events

据我了解:

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

订阅了 localStorage 和 sessionStorage 事件。 我可以只订阅 localStorage 事件吗?

谢谢。

我认为你做不到,正如你所说 storagefired on the window when any storage item changes。您只需要在收到事件时检查事件的 storageArea 属性,而忽略会话存储中的事件。例如:

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