Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Can I subscribe on localStorage events only?

Thanks.

like image 678
Vitaly Avatar asked Oct 06 '15 08:10

Vitaly


People also ask

What event is fired after web storage update?

A storage event is fired when you insert, update or delete a sessionStorage or localStorage property.

Why we should not use localStorage?

Why Local Storage is Insecure and You Shouldn't Use it to Store Sensitive Data. Here's the deal: most of the bad things about local storage aren't all that important. You can still get away with using it but you'll just have a slightly slower app and minor developer annoyance.

Which is better sessionStorage vs localStorage?

For most cases, we use the localStorage object if we want some data to be on the browser. If we want it on the server, then we use cookies, and the sessionStorage is used when we want to destroy the data whenever that specific tab gets closed or the season is closed by the user.


1 Answers

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);
like image 162
T.J. Crowder Avatar answered Sep 20 '22 18:09

T.J. Crowder