Javascript Browser Storage length Property via session storage

Introduction

The following code uses session storage instead of local storage.

Get the number of session storage item for this domain:

var x = sessionStorage.length;

This example demonstrates how to use the length property to get the number of items stored in the session storage object.

You might not have any items stored in your session storage and the following code added a script that creates some.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="createItems()">Create session storage items</button>
<p>Click the button to get the number of session storage items:</p>

<button onclick="myFunction()">Get number of items</button>

<p id="demo"></p>

<script>

function createItems() {/*  w  w  w.j a  v a2s.  c  o  m*/
  sessionStorage.test1 = "hello";
  sessionStorage.test2 = "java2s.com";
  sessionStorage.test3 = 42;
}

function myFunction() {
  var x = sessionStorage.length;
  document.getElementById("demo").innerHTML = x;
}

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



PreviousNext

Related