Javascript Browser Storage key() Method by session storage

Introduction

Using session storage instead of local storage.

Get the name of the first storage item:

var x = sessionStorage.key(0);

This example demonstrates how to use the key() method to get the name of a session storage item.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="createItems()">Create session storage items</button>
<button onclick="myFunction()">Get item name</button>
<p id="demo"></p>
<script>

function createItems() {//from w ww  .  j  a  v  a 2  s. c  om
  sessionStorage.test1 = "hello";
  sessionStorage.test2 = "Jim";
  sessionStorage.test3 = 358;
}

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

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



PreviousNext

Related