Thí dụ
Đặt và truy xuất một cặp/cặp giá trị SessionStorage:
sessionStorage.setItem ["LastName", "Smith"]; sessionStorage.getItem ["lastName"];
sessionStorage.getItem["lastname"];
Hãy tự mình thử »
Thêm ví dụ dưới đây.
Định nghĩa và cách sử dụng
Đối tượng sessionStorage
cho phép bạn lưu trữ các cặp khóa/giá trị trong trình duyệt.
Ghi chú
Đối tượng sessionStorage
lưu trữ dữ liệu chỉ trong một phiên.
[Dữ liệu bị xóa khi trình duyệt được đóng].
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 trình duyệt được đóng và có sẵn trong các phiên trong tương lai].
Cú pháp
cửa sổ.sessionStorage
Hoặc chỉ:
Phiên điều hành
Lưu dữ liệu vào lưu trữ phiên
sessionStorage.setItem ["khóa", "value"];
Đọc dữ liệu từ lưu trữ phiên
Đặt lastName = sessionStorage.getItem ["khóa"];
Xóa dữ liệu khỏi lưu trữ phiên
sessionStorage.RemoveItem ["khóa"];
Xóa tất cả [Xóa lưu trữ phiên]
sessionStorage.clear [];
Thông số
Tham số | Sự mô tả |
Chìa khóa | Yêu cầu. Tên của một khóa. The name of a key. |
giá trị | Yêu cầu. Giá trị của khóa. The value of the key. |
Giá trị trả lại
Loại hình | Sự mô tả |
Chìa khóa | Yêu cầu. Tên của một khóa. |
giá trị
Yêu cầu. Giá trị của khóa.
Giá trị trả lại
sessionStorage.clickcount = Number[sessionStorage.clickcount] + 1;
} else {
sessionStorage.clickcount = 1;
}
Hãy tự mình thử »
Loại hình
Sự vật
Một đối tượng sessionStorage. | Nhiều ví dụ hơn | Đếm số lần người dùng đã nhấp vào nút: | if [sessionStorage.clickCount] {& nbsp; sessionStorage.clickCount = number [sessionStorage.clickCount] + 1;} other {& nbsp; & nbsp; sessionStorage.clickCount = 1;} | Hỗ trợ trình duyệt | window.sessionStorage được hỗ trợ trong tất cả các trình duyệt: |
Trình duyệt Chrome | I E | Trình duyệt Chrome | Trình duyệt Chrome | Trình duyệt Chrome | Trình duyệt Chrome |
Thuộc tính Dữ liệu được lưu trữ trong Các khóa và các giá trị luôn ở định dạng chuỗi UTF-16, sử dụng hai byte trên mỗi ký tự. Cũng như các đối tượng, các khóa số nguyên được tự động chuyển đổi thành chuỗi. Một đối tượng Bị ném vào một trong những trường hợp sau: Lưu ý rằng nếu người dùng chặn cookie, các trình duyệt có thể sẽ diễn giải đây như một hướng dẫn để ngăn trang không thể tồn tại.sessionStorage
chỉ đọc truy cập vào một đối tượng phiên Storage
cho nguồn gốc hiện tại. sessionStorage
tương tự như localStorage
; Sự khác biệt là trong khi dữ liệu trong localStorage
không hết hạn, dữ liệu trong sessionStorage
sẽ bị xóa khi phiên trang kết thúc.sessionStorage
property accesses a session Storage
object for the current origin. sessionStorage
is similar to localStorage
; the difference
is that while data in localStorage
doesn't expire, data in sessionStorage
is cleared when the page session ends. sessionStorage
cho mỗi tab/cửa sổ.sessionStorage
của tab vào tab mới.sessionStorage
.sessionStorage
là cụ thể cho giao thức của trang. Cụ thể, dữ liệu được lưu trữ bởi một tập lệnh trên một trang web được truy cập với http [ví dụ: //example.com] được đặt trong một đối tượng ____22 khác từ cùng một trang web được truy cập với https [ví dụ: //example.com] .is specific to the protocol of the page. In particular, data stored by a
script on a site accessed with HTTP [e.g., //example.com] is put in a different sessionStorage
object from the same site accessed with HTTPS [e.g., //example.com]. Giá trị
Storage
có thể được sử dụng để truy cập không gian lưu trữ phiên của Origin.Ngoại lệ
7// Get the text field that we're going to track
let field = document.getElementById["field"];
// See if we have an autosave value
// [this will only happen if the page is accidentally refreshed]
if [sessionStorage.getItem["autosave"]] {
// Restore the contents of the text field
field.value = sessionStorage.getItem["autosave"];
}
// Listen for changes in the text field
field.addEventListener["change", [] => {
// And save the results into the session storage object
sessionStorage.setItem["autosave", field.value];
}];
8 hoặc // Get the text field that we're going to track
let field = document.getElementById["field"];
// See if we have an autosave value
// [this will only happen if the page is accidentally refreshed]
if [sessionStorage.getItem["autosave"]] {
// Restore the contents of the text field
field.value = sessionStorage.getItem["autosave"];
}
// Listen for changes in the text field
field.addEventListener["change", [] => {
// And save the results into the session storage object
sessionStorage.setItem["autosave", field.value];
}];
9 chẳng hạn.// Get the text field that we're going to track
let field = document.getElementById["field"];
// See if we have an autosave value
// [this will only happen if the page is accidentally refreshed]
if [sessionStorage.getItem["autosave"]] {
// Restore the contents of the text field
field.value = sessionStorage.getItem["autosave"];
}
// Listen for changes in the text field
field.addEventListener["change", [] => {
// And save the results into the session storage object
sessionStorage.setItem["autosave", field.value];
}];
Ví dụ
Cách sử dụng cơ bản
// Save data to sessionStorage
sessionStorage.setItem["key", "value"];
// Get saved data from sessionStorage
let data = sessionStorage.getItem["key"];
// Remove saved data from sessionStorage
sessionStorage.removeItem["key"];
// Remove all saved data from sessionStorage
sessionStorage.clear[];
Lưu văn bản giữa các lần làm mới
Ví dụ sau đây tự động tạo nội dung của trường văn bản và nếu trình duyệt được làm mới, hãy khôi phục nội dung trường văn bản để không bị mất.
// Get the text field that we're going to track
let field = document.getElementById["field"];
// See if we have an autosave value
// [this will only happen if the page is accidentally refreshed]
if [sessionStorage.getItem["autosave"]] {
// Restore the contents of the text field
field.value = sessionStorage.getItem["autosave"];
}
// Listen for changes in the text field
field.addEventListener["change", [] => {
// And save the results into the session storage object
sessionStorage.setItem["autosave", field.value];
}];
Thông số kỹ thuật
HTML Standard # Dom-SessionStorage-Dev # dom-sessionstorage-dev |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt