Storage.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.

Storage 接口的只读属性 length 返回存储在一个给定的 Storage 对象中的数据项的数量。

Storage 对象中存储的数据项数量。

示例

下面的函数添加三个数据项到当前域名的本地存储里面,然后返回本地存储里面数据项的数量:

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

  return localStorage.length; // 应该返回 3
}

备注: 有关实际运行的例子,详见 Web Storage 演示

规范

Specification
HTML Standard
# dom-storage-length-dev

浏览器兼容性

BCD tables only load in the browser

相关链接