Set and retrieve localStorage name/value pair:
localStorage.setItem("lastname", "Smith");
localStorage.getItem("lastname");
Try it Yourself »
More examples below.
The localStorage
object allows you to save key/value pairs in the browser.
The localStorage object stores data with no expiration date.
The data is not deleted when the browser is closed, and are available for future sessions.
The sessionStorage Object which stores data for one session.
(The data is deleted when the browser window is closed)
window.localStorage
localStorage
localStorage.setItem(
key,
value);
let lastname = localStorage.getItem(
key);
localStorage.removeItem(
key);
localStorage.clear();
Parameter | Description |
key | Required. The name of a key. |
value | Required. The value of the key. |
Type | Description |
An object | A localStorage object. |
Count the number of times a user has clicked a button:
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
Try it Yourself »
window.localStorage
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!