Window sessionStorage property

Definition and Usage

localStorage and sessionStorage The attribute allows key/value pairs to be saved in web browsers.

sessionStorage The object only stores data for one session (the data will be deleted when the browser tab is closed).

Tip:See also localStorage propertyThis property stores data without an expiration date. The data will not be deleted when the browser is closed and will be available the next day, a week, or a year later.

Example

Example 1

Create a sessionStorage name/value pair with name="lastname" and value="Smith", then retrieve the value of "lastname" and insert it into the element with id="result":

// Store
sessionStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = sessionStorage.getItem("lastname");

Try it yourself

Example 2

The following example calculates the number of times a user clicks the button in the current session:

if (sessionStorage.clickcount) {
  sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
  sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.";

Try it yourself

Syntax

window.sessionStorage

Syntax for saving data to sessionStorage:

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

Syntax for reading data from sessionStorage:

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

Syntax for deleting data from sessionStorage:

sessionStorage.removeItem("key");

Syntax for deleting all saved data from sessionStorage:

sessionStorage.clear();

Technical details

Return value: Storage Object

Browser Support

The numbers in the table specify the first browser version that fully supports this property.

Properties Chrome IE Firefox Safari Opera
sessionStorage 4.0 8.0 3.5 4.0 11.5