local​Storage用法

2019-05-21  本文已影响0人  ALLIN5

语法

myStorage = localStorage;

一个可被用于访问当前源( origin )的本地存储空间的 Storage 对象。

异常

<dl style="font-style: normal; margin: 0px 0px 20px; padding: 0px; border: 0px; box-sizing: border-box; max-width: 42rem; color: rgb(51, 51, 51); font-family: Arial, x-locale-body, sans-serif; font-size: 16px; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.04448px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">

<dt style="font-style: normal; margin: 0px; padding: 0px; border: 0px; font-weight: 700;">`SecurityError`</dt>

<dd style="font-style: normal !important; margin: 0px 0px 24px; padding: 0px 0px 0px 20px; border: 0px;">请求违反了一个策略声明,或者源( origin )不是 [一个有效的 scheme/host/port tuple](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#Definition_of_an_origin) (例如如果origin使用 `file:` 或者 `data:` 形式将可能发生)。比如,用户可以有禁用允许对指定的origin存留数据的浏览器配置。</dd>

</dl>

示例

下面的代码片段访问了当前域名下的本地 Storage 对象,并通过 Storage.setItem() 增加了一个数据项目。

localStorage.setItem('myCat', 'Tom');

该语法用于读取 localStorage 项,如下:

let cat = localStorage.getItem('myCat');

该语法用于移除 localStorage 项,如下:

localStorage.removeItem('myCat');

该语法用于移除所有的 localStorage 项,如下:

// 移除所有
localStorage.clear();
上一篇下一篇

猜你喜欢

热点阅读