Window sessionStorage
Thí dụ
Đặt và truy xuất cặp giá trị / tên sessionStorage:
sessionStorage.setItem("lastname", "Smith");
sessionStorage.getItem("lastname");
Thêm ví dụ bên dưới.
Định nghĩa và Cách sử dụng
Đối sessionStorage
tượng cho phép bạn lưu trữ các cặp khóa / giá trị trong trình duyệt.
Ghi chú
Đối sessionStorage
tượng chỉ lưu trữ dữ liệu cho một phiên.
(Dữ liệu bị xóa khi đóng trình duyệt).
Xem thêm:
Đối tượng localStorage lưu trữ dữ liệu không có ngày hết hạn.
(Dữ liệu không bị xóa khi đóng trình duyệt và có sẵn trong các phiên trong tương lai).
Cú pháp
window.sessionStorage
Hoặc chỉ:
sessionStorage
LƯU dữ liệu vào phiên
sessionStorage.setItem("key", "value");
ĐỌC dữ liệu từ sessionStorage:
let lastname = sessionStorage.getItem("key");
XÓA dữ liệu khỏi phiên
sessionStorage.removeItem("key");
XÓA phiên
sessionStorage.clear();
Thông số
Parameter | Description |
key | Required. The name of a key. |
value | Required. The value of the key. |
Giá trị trả lại
Loại | Sự miêu tả |
Sự vật | Một đối tượng sessionStorage. |
Các ví dụ khác
Đếm số lần người dùng đã nhấp vào nút:
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount)
+ 1;
} else {
sessionStorage.clickcount = 1;
}
Hỗ trợ trình duyệt
window.sessionStorage
được hỗ trợ trong tất cả các trình duyệt:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 8-11 | Yes | Yes | Yes | Yes |