Javascript Browser Storage removeItem() Method by session storage

Introduction

Using session storage instead of local storage.

Remove the specified session storage item:

sessionStorage.removeItem("test1");

This example demonstrates how to use the removeItem() method to delete a specified session storage item.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="createItem()">Create session storage item</button>
<button onclick="deleteItem()">Delete item</button>
<button onclick="displayItem()">Display item</button>
<p id="demo"></p>

<script>

function createItem() {/*from  ww  w  .j ava 2s  . c om*/
  sessionStorage.setItem("test1", "hello");
}

function deleteItem() {
  sessionStorage.removeItem("test1");
}

function displayItem() {
  var x = sessionStorage.getItem("test1");
  document.getElementById("demo").innerHTML = x;
}

</script>
</body>
</html>



PreviousNext

Related