THE WORLD'S LARGEST WEB DEVELOPER SITE

Window sessionStorage Property

❮ Window Object

Example

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 »

More "Try it Yourself" examples below.


Definition and Usage

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).

Tip: Also look at the localStorage property which stores data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.


Browser Support

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

Property
sessionStorage 4.0 8.0 3.5 4.0 11.5


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 REMOVING saved data from sessionStorage:

sessionStorage.removeItem("key");

Syntax for REMOVING ALL saved data from sessionStorage:

sessionStorage.clear();

Technical Details

Return Value: A Storage object

More Examples

Example

The following example counts the number of times a user has clicked a 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 »

❮ Window Object