此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

StorageManager.persist()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2021年12月⁩.

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

**persist()**方法是StorageManager的一个接口,用于请求本地数据存储的权限,如果被授予权限,则返回一个 resolved 状态值为truePromise对象,否则返回false

语法

navigator.storage.persist().then(function(persistent) { ... })

参数

返回值

一个 resolved 状态,值为Boolean类型的Promise对象。

示例

js
if (navigator.storage && navigator.storage.persist)
  navigator.storage.persist().then(function (persistent) {
    if (persistent)
      console.log("Storage will not be cleared except by explicit user action");
    else
      console.log("Storage may be cleared by the UA under storage pressure.");
  });

规范

Specification
Storage
# dom-storagemanager-persist

浏览器兼容性