Javascript DOM StorageEvent key Property

Introduction

Get the name of the storage item that changed.

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

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

The key property belongs to the Storage Event object.

It returns the name of the item that was changed.

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 ww.  j  a v a  2 s . c  o  m*/
  var x = "Key: " + event.key;
  document.getElementById("demo").innerHTML = x;
}

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

The key property returns the name of the changed storage item.

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




PreviousNext

Related