English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Window localStorage Properties

JavaScript Window Object

localStorageRead-only properties allow you to access aStorageFor the source object of the document; the data stored is saved across browser sessions.

Compared to using cookies, the localStorage object provides a mechanism through which browsers can store keys in a better way./Value pairs.

localStorage compared tosessionStorageSimilarly, the difference is that although the data stored in localStorage does not have an expiration time, the data stored in sessionStorage will be cleared when the page session ends (i.e., when the page is closed).

You can find similar information in ourHTML5 In the Web Storage APIFor more information about localStorage, please visit.

Syntax:

window.localStorage

To store dataSaveThe syntax for saving data to localStorage is:

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

from localStorage ReadData syntax:

var name = localStorage.getItem("key");

from localStorage DeleteData syntax:

localStorage.removeItem("key");
// Store
localStorage.setItem("name", "Parrot");
// Retrieve
document.getElementById("demo").innerHTML = localStorage.getItem("name");
Test and See‹/›

The above code creates a localStorage name using name="name" and value="Parrot"./Value pairs.

Then retrieve the value of 'Name' and insert it into the element with id="demo".

Name/Pairs are always stored as strings, and you can convert them to another format as needed.

Browser Compatibility

The numbers in the table specify the first browser version to fully support the localStorage property:

Properties
localStorage43511549

Technical Details

Return Value:OneStorageObject, used to access the local storage space of the current origin

More Examples

The following example sets a local storage variable and accesses the variable each time the page is visited:

if (localStorage.hits) {
localStorage.hits = Number(localStorage.hits) + 1;
} else {
localStorage.hits = 1;
}
Test and See‹/›

You can also delete localStorage items:

localStorage.removeItem("hits");
Test and See‹/›

Related References

Window (Window) Reference:window.sessionStorage property

HTML Tutorial:HTML5 Web Storage API

JavaScript Window Object