Javascript Browser Storage clear() Method by session storage

Introduction

Using session storage instead of local storage.

Remove all session items:

sessionStorage.clear();

This example demonstrates how to use the clear() method to delete all session storage items for this domain.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="createItems()">Create session storage items</button>
<button onclick="deleteItems()">Delete items</button>
<button onclick="displayItems()">Display items</button>
<p id="demo"></p>

<script>
function createItems() {/*from  w w w  . ja  v a  2 s .  c  om*/
  sessionStorage.setItem("mytime", Date.now());
  sessionStorage.setItem("myname", "CSS");
  sessionStorage.setItem("myage", 36);
}

function deleteItems() {
  sessionStorage.clear();
}

function displayItems() {
  var l, i;
  document.getElementById("demo").innerHTML = "";
  for (i = 0; i < sessionStorage.length; i++) {
      x = sessionStorage.key(i);
      document.getElementById("demo").innerHTML += x + "<br>";
  }
}
</script>

</body>
</html>



PreviousNext

Related