Window sessionStorage-eigenschap

Definitie en gebruik

localStorage en sessionStorage Eigenschap maakt het mogelijk om sleutel/waardeparen op te slaan in een webbrowser.

sessionStorage Objects only store data for one session (data will be deleted when the browser tab is closed).

Tip:See also localStorage propertyThis property stores data without an expiration date. The data is not 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 the user has clicked 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 + " times 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 indicate the first browser version that fully supports the property.

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