Window sessionStorageProperty

JavaScript 存储对象JavaScript Storage Object

Examples

Use sessionStorage to create a local storage name/value pair, name="lastname" 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 »

Definition and Usage

The localStorage and sessionStorage properties allow storing key/value pair data in the browser.

sessionStorage is used to temporarily save data for the same window (or tab), and this data will be deleted after the window or tab is closed.

Tip:If you want to keep data after the browser window is closed, you can use the localStorageproperty. This data object has no expiration time; it can be used today, next week, or next year, unless you manually delete it.


Browser Support

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

Property
sessionStorage 4.0 8.0 3.5 4.0 11.5

Syntax

window.sessionStorage

Syntax for saving data:

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

Syntax for reading data:

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

Syntax for deleting data of a specified key:

sessionStorage.removeItem("key");

Delete all data:

sessionStorage.clear();

Technical Details

Return Value: A storage object

More Examples

Example

The following example is used to record 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.";

Try it Yourself »

JavaScript 存储对象JavaScript Storage Object

Other Extensions