Storage event Listener not working properly in Incognito or private Window

Viewed 1160

In normal browsers the Storage event Listener working and triggering properly

Event listener code is given below

window.addEventListener('storage', function (e) {
      console.log("storage event occured here");
},false);

Storage change by using

 localStorage.setItem('logout-event', 'logout'+Math.random());

When any changes made in local storage its trigger other browser tab storage event properly. But when I tried same code in Incognito or Private window its working propely in chrome and mozilla browsers but not in Edge and Internet Explorer browsers

But in case Of IE, Edge and Brave browser private windows this event is not triggered in my website. In other browsers its working as per expected. Is there any way to make it work in the incognito window of IE and Edge browsers

1 Answers

The storage event occurs when there is a change in the window's change area.

Note: The storage event is only triggered when a window other than itself makes the changes.

You could check the following sample code:

<button onclick="changeValue()">Change a Storage Item</button>

<p id="demo"></p>

<script>
    window.addEventListener("storage", function myFunction(event) {
        document.getElementById("demo").innerHTML = "A change was made in the storage area";
        console.log("storage event occured here");
    }, false);

    function changeValue() {
        var x = window.open("popuppage.html", "myWindow", "width=200,height=100");
        x.localStorage.setItem("mytime", Date.now());
        x.close();
    }
</script>

When open a new window and change the local storage, it will trigger the storage event. The above code works well on my side (using IE 11 and MS Edge 44 version).

More detail about storage event, please check this article.

Related