Javascript Browser Storage clear() Method

Introduction

Remove all local storage items:

localStorage.clear();

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

View in separate window

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

<script>
function createItems() {/*from   www.  java2 s  .  co  m*/
  localStorage.setItem("mytime", Date.now());
  localStorage.setItem("myname", "CSS");
  localStorage.setItem("myage", 36);
}

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

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

</body>
</html>

The clear() method removes all the Storage Object item for this domain.

The clear() method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object.

clear();



PreviousNext

Related