Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Window sessionStorage property

Window sessionStorage property

JavaScript Storage Objects JavaScript Storage Object

Example

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

//

Storage

sessionStorage

.

setItem

(

"

lastname

"

,

"

Smith

"

)

;

//

Retrieval

document

.

getElementById

(

"

result

"

)

.

innerHTML

=

sessionStorage

.

getItem

(

"

lastname

"

)

;

Try it »


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); the data is deleted after the window or tab is closed.

Tip: If you want to keep data even after the browser window is closed, you can use the localStorage property; this data object has no expiration time and 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 this 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 with a specified key:

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.

"

;

Try it »

JavaScript Storage Objects JavaScript Storage Object