Session Storage is an HTML 5 browser standard to enable storage and retrieval of simple data on the client. This is a global object (sessionStorage) that maintains a storage area that's available for the duration of the page session. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated.
Session Storage enables storage and retrieval of key-value-pairs in the browser, that endure only as long as the session.
The W3 specification can be viewed here:
https://html.spec.whatwg.org/multipage/webstorage.html
https://www.w3.org/TR/webstorage/For more details, see the local-storage tag wiki on Stack Overflow:
https://stackoverflow.com/tags/local-storage/info
The sessionStorage
Object
The sessionStorage
object is equal to the localStorage
object, except that it stores the data for only one session. The data is deleted when the user closes the browser window.
Only plain-text values can be stored. Arrays, hashes, numbers, strings and booleans can be stored by using JSON.stringify(value)
. Then, to get the original value when reading the value, use JSON.parse(stringified_value)
.
Example
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.";