blob: 1733a325bdd6db45450cf6d03917e14ec135e2d1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
---
title: Window.localStorage
slug: Web/API/Window/localStorage
translation_of: Web/API/Window/localStorage
---
<p>{{APIRef("Web Storage API")}}</p>
<p><span class="seoSummary"><strong><code>localStorage</code></strong> 為一唯讀屬性, 此屬性允許您存取目前文件({{DOMxRef("Document")}})隸屬網域來源的 {{DOMxRef("Storage")}} 物件; 與 sessionStorage 不同的是其儲存資料的可存取範圍為跨瀏覽頁狀態(Browser Sessions).</span> <code>localStorage</code> 的應用與 {{DOMxRef("Window.sessionStorage", "sessionStorage")}} 相似, 除了 <code>localStorage</code> 的儲存資料並無到期的限制, 而 <code>sessionStorage</code> 的儲存資料於目前瀏覽頁狀態結束的同時將一併被清除 — 也就是目前瀏覽器頁面被關閉的同時.</p>
<p>值得注意的是不論 <code>localStorage</code> 或者 <code>sessionStorage</code> <strong>皆為專屬於目前瀏覽器頁面的通訊協定(Protocol)</strong>.</p>
<p>鍵值名稱和值皆為<strong>字串型式</strong>(請留意, 當其為物件, 整數等將自動轉換為字串型式).</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox"><em>myStorage</em> = <em>window</em>.localStorage;</pre>
<h3 id="Value">Value</h3>
<p>{{DOMxRef("Storage")}} 物件 which can be used to access the current origin's local storage space.</p>
<h3 id="Exceptions">Exceptions</h3>
<dl>
<dt><code>SecurityError</code></dt>
<dd>The request violates a policy decision, or the origin is not <a href="/en-US/docs/Web/Security/Same-origin_policy#Definition_of_an_origin">a valid scheme/host/port tuple</a> (this can happen if the origin uses the <code>file:</code> or <code>data:</code> scheme, for example). 舉例來說,使用者 may have their browser configured to deny permission to persist data for the specified origin.</dd>
</dl>
<h2 id="Example">Example</h2>
<p>下列的程式碼片段讀取了目前域名內的 local {{DOMxRef("Storage")}} 物件 ,並用{{DOMxRef("Storage.setItem()")}},增加一個資料物件 item 到其中</p>
<pre class="brush: js">localStorage.setItem('myCat', 'Tom');</pre>
<p>讀取 <code>localStorage</code> 內物件的語法如下:</p>
<pre class="brush: js">var 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">// Clear all items
localStorage.clear();
</pre>
<div class="note">
<p><strong>Note</strong>: Please refer to the <a href="/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">Using the Web Storage API</a> article for a full example.</p>
</div>
<h2 id="Specifications">Specifications</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("HTML WHATWG")}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{Compat("api.Window.localStorage")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API">Using the Web Storage API</a></li>
<li><a href="/en-US/docs/Web/API/Web_Storage_API/Local_storage">Local storage with Window.localStorage</a></li>
<li><span>{{DOMxRef("Window.sessionStorage")}}</span></li>
</ul>
|