localStorage eventListener is not called [duplicate]
Asked Answered
C

2

26

I added an eventListener to the window DOM-Object and want to keep track of the changes made to localStorage.

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

    <script language="JavaScript"><!-- 
        window.addEventListener('storage', storageEventHandler, false);
        function storageEventHandler(evt){
            console.log("oldValue: " + evt.oldValue );
            console.log("storage event called key: " + evt.key );
            console.log("newValue: " + evt.newValue );

        }
        $(document).ready(function(event) {
            $('#link1').click(function(event){
                event.preventDefault();
                localStorage.setItem('page', 2000);
                console.log(localStorage.getItem('page'));
            });
            $('#link2').click(function(event){
                event.preventDefault();
                localStorage.setItem('page', 998);
                console.log(localStorage.getItem('page'));

            });

         });
    </script>
</head>
</html>

Somehow the storageEventHandler is never called even though the localStorage value is changed when I click link1 or link2. Any help is much appreciated.

Cleptomania answered 20/3, 2011 at 19:13 Comment(3)
whether or not the storage event gets fired, depends on the browser.Cords
That's correct, see e.g. code.google.com/p/chromium/issues/detail?id=48159.Keefer
in react i used window.dispatchEvent(new Event("storage")) after changing storage. this will call listeners manuallyCalida
B
52

Some browsers don't support the storage event, and most of the browsers that do support it will only call it when the storage is changed by a different window/tab. So, open your page up in two windows. Click the links in one window and you will probably see the event in the other.

The assumption is that your page will already know all interactions with localStorage in its own window and only needs notification when a different window changes things. This, of course, is a foolish assumption. But, localStorage is a new thing. Hopefully, they'll eventually figure it all out.

Bettis answered 27/7, 2011 at 14:33 Comment(6)
unfortunately, the aforementioned time "eventually" hasn't come yet :(Irresolute
Sadly, that's true. I hope to make time to revisit this eventually. I have some wild hack ideas for working around some of these things. I just don't have much time for such experiments these days.Bettis
html5demos.com/storage-eventsContribute
7 years later and this still doesn't work as one would assume.Pedro
Not sure why but the "eventually" part hasn't been fixed yet. IMO, that could have been a very useful feature.Bradberry
@Pedro Make that ten yearsVinita
G
32

The storage event handler will only affect other windows. Whenever something changes in one window inside localStorage all the other windows are notified about it and if any action needs to be taken it can be achieved by a handler function listening to the storage event.

For same window you have to manually call the storageEventHandler function after localStorage.setItem() is called to achieve the same behaviour in the same window.

Gains answered 26/9, 2013 at 5:19 Comment(2)
note: IE10 : The storage event is fired even on the originating document where it occurred. resourceMelancholy
can U provide an example, im lost with your answer developer.mozilla.org/en-US/docs/Web/API/Window/storage_eventDiffractive

© 2022 - 2024 — McMap. All rights reserved.