Easy Tutorial
❮ Prop Anchor Hreflang Event Button ❯

Window sessionStorage Property

JavaScript Storage Object

Example

Use sessionStorage to create a locally stored 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");

Definition and Usage

The localStorage and sessionStorage properties allow the storage of key/value pairs in the browser.

sessionStorage is used to temporarily save data for the same window (or tab). The data is deleted when the window or tab is closed.

Tip: If you want to retain data after the browser window is closed, you can use the localStorage property, which keeps the data without an expiration time and is available today, next week, next year, unless manually deleted.


Browser Support

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

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

Syntax

window.sessionStorage

Save data syntax:

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

Read data syntax:

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

Delete specific key data syntax:

sessionStorage.removeItem("key");

Delete all data:

sessionStorage.clear();

Technical Details

| Return Value: | A storage object |


More Examples

Example

The following example counts the number of times a button is clicked:

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.";

JavaScript Storage Object

❮ Prop Anchor Hreflang Event Button ❯