Storage: thuộc tính length

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Thuộc tính chỉ đọc length của giao diện Storage trả về số lượng mục dữ liệu được lưu trong một đối tượng Storage nhất định.

Giá trị

Số lượng mục được lưu trong đối tượng Storage.

Ví dụ

Hàm sau thêm ba mục dữ liệu vào local storage của miền hiện tại, rồi trả về số lượng mục trong bộ lưu trữ:

js
function populateStorage() {
  localStorage.setItem("bgcolor", "yellow");
  localStorage.setItem("font", "Helvetica");
  localStorage.setItem("image", "cats.png");

  return localStorage.length; // Nên trả về 3
}

Note: Để xem ví dụ thực tế, hãy xem Web Storage Demo.

Thông số kỹ thuật

Specification
HTML
# dom-storage-length-dev

Tương thích trình duyệt

Xem thêm