diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/window/localstorage | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/window/localstorage')
| -rw-r--r-- | files/zh-cn/web/api/window/localstorage/index.html | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/window/localstorage/index.html b/files/zh-cn/web/api/window/localstorage/index.html new file mode 100644 index 0000000000..0145b13bc7 --- /dev/null +++ b/files/zh-cn/web/api/window/localstorage/index.html @@ -0,0 +1,93 @@ +--- +title: Window.localStorage +slug: Web/API/Window/localStorage +tags: + - API + - Storage + - Window + - localStorage + - 只读属性 +translation_of: Web/API/Window/localStorage +--- +<p>{{APIRef()}}</p> + +<p>只读的<span class="seoSummary"><code>localStorage</code> 属性允许你访问一个{{domxref("Document")}} 源(origin)的对象 {{domxref("Storage")}};</span>存储的数据将保存在浏览器会话中<span class="seoSummary">。</span><code>localStorage</code> 类似 {{DOMxRef("Window.sessionStorage", "sessionStorage")}},但其区别在于:存储在 <code>localStorage</code> 的数据可以长期保留;而当页面会话结束——也就是说,当页面被关闭时,存储在 <code>sessionStorage</code> 的数据会被清除 。</p> + +<p>应注意,无论数据存储在 <code>localStorage</code> 还是 <code>sessionStorage</code> ,<strong>它们都特定于页面的协议。</strong></p> + +<p>另外,<code>localStorage</code> 中的键值对总是以字符串的形式存储。 (需要注意, 和js对象相比, 键值对总是以字符串的形式存储意味着数值类型会自动转化为字符串类型).</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">myStorage = localStorage;</pre> + +<h3 id="值">值</h3> + +<p>一个可被用于访问当前源( origin )的本地存储空间的 {{domxref("Storage")}} 对象。</p> + +<h3 id="异常">异常</h3> + +<dl> + <dt><code>SecurityError</code></dt> + <dd>请求违反了一个策略声明,或者源( origin )不是 <a href="/en-US/docs/Web/Security/Same-origin_policy#Definition_of_an_origin">一个有效的 scheme/host/port tuple</a> (例如如果origin使用 <code>file:</code> 或者 <code>data:</code> 形式将可能发生)。比如,用户可以有禁用允许对指定的origin存留数据的浏览器配置。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<p>下面的代码片段访问了当前域名下的本地 {{domxref("Storage")}} 对象,并通过 {{domxref("Storage.setItem()")}} 增加了一个数据项目。</p> + +<pre class="brush: js">localStorage.setItem('myCat', 'Tom'); +</pre> + +<p>该语法用于读取 <code>localStorage</code> 项,如下:</p> + +<pre class="brush: js">let cat = localStorage.getItem('myCat'); +</pre> + +<p>该语法用于移除 <code>localStorage</code> 项,如下:</p> + +<pre class="brush: js">localStorage.removeItem('myCat'); +</pre> + +<p>该语法用于移除所有的 <code>localStorage</code> 项,如下:</p> + +<pre class="brush: js">// 移除所有 +localStorage.clear(); +</pre> + +<div class="note"> +<p><strong>注意</strong>: 请参考 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">Using the Web Storage API</a> 的完整示例文章。</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', 'webstorage.html#dom-localstorage', 'localStorage')}}</td> + <td>{{Spec2('Web Storage')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Window.localStorage")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">使用 Web Storage API</a></li> + <li><a href="/zh-CN/docs/Web/API/Web_Storage_API/Local_storage">Local storage(Window.localStorage)</a></li> + <li>{{domxref("LocalStorage")}}</li> + <li>{{domxref("SessionStorage")}}</li> + <li>{{domxref("Window.sessionStorage")}}</li> +</ul> |
