×

Save Your Code

If you click the save button, your code will be saved, and you get an URL you can share with others.

By clicking the "Save" button you agree to our terms and conditions.

Report Error

×

Save to Google Drive

If you have a Google account, you can save this code to your Google Drive.

Google will ask you to confirm Google Drive access.

×

Open from Google Drive

If you have saved a file to Google Drive, you can open it here:

Result Size: 625 x 571
x
 
<!DOCTYPE html>
<html>
<body>
<h1>The Storage clear() Method</h1>
<p>This example demonstrates how to use the clear() method to delete all the local storage items for this domain.</p>
<h2>Missing localStorage items?</h2>
<p>Since you might not have any items stored in your local storage, we have added a script that creates some for you.</p>
<button onclick="createItems()">Create local storage items</button>
<h2>Remove Items</h2>
<p>Click the button to delete the items:</p>
<button onclick="deleteItems()">Delete items</button>
<h2>Display Items</h2>
<p>Click the button to display all items:</p>
<button onclick="displayItems()">Display items</button>
<p id="demo"></p>
<script>
function createItems() {
  localStorage.setItem("mytime", Date.now());
  localStorage.setItem("myname", "John");
  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>";
  }
×

Report a Problem: