Javascript Browser Window sessionStorage Property

Description

Javascript Browser Window sessionStorage Property

View in separate window

<!DOCTYPE html>
<html>
<body>

<div id="result"></div>

<script>
// Check browser support
if (typeof(Storage) !== "undefined") {
  // Store/*from   w w  w  . j a va2 s .co  m*/
  sessionStorage.setItem("lastname", "Smith");
  // Retrieve
  document.getElementById("result").innerHTML = sessionStorage.getItem("lastname");
} else {
  document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>

</body>
</html>

The localStorage and sessionStorage properties allow to save key/value pairs in a web browser.

The sessionStorage object stores data for only one session.

The data is deleted when the browser tab is closed.

to save data to sessionStorage:

sessionStorage.setItem("key", "value");

to read data from sessionStorage:

var lastname = sessionStorage.getItem("key");

To remove saved data from sessionStorage:

sessionStorage.removeItem("key");

To remove all saved data from sessionStorage:

sessionStorage.clear();



PreviousNext

Related