Javascript DOM storage Event

Introduction

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

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

window.addEventListener("storage", myFunction);

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="changeValue()">Change a Storage Item</button>
<p id="demo"></p>
<script>
window.addEventListener("storage", myFunction);

function myFunction(event) {//from  w  w  w .j av  a2s .  c  om
  document.getElementById("demo").innerHTML = "A change was made in the storage area";
}

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

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

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

Bubbles: No
Cancelable: No
Event type: StorageEvent
DOM Version: Web Storage



PreviousNext

Related