You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Web storage objects `localStorage`and`sessionStorage`allow to save key/value pairs in the browser.
3
+
Web 存储对象 `localStorage`和`sessionStorage`允许你在浏览器上保存键值对。
4
4
5
-
What's interesting about them is that the data survives a page refresh (for `sessionStorage`) and even a full browser restart (for `localStorage`). We'll see that very soon.
-Unlike cookies, web storage objects are not sent to server with each request. Because of that, we can store much more. Most browsers allow at least 2 megabytes of data (or more) and have settings to configure that.
10
-
-Also unlike cookies, the server can't manipulate storage objects via HTTP headers. Everything's done in JavaScript.
11
-
-The storage is bound to the origin (domain/protocol/port triplet). That is, different protocols or subdomains infer different storage objects, they can't access data from each other.
Both storage objects provide same methods and properties:
13
+
两个存储对象都提供相同的方法和属性:
14
14
15
-
-`setItem(key, value)` -- store key/value pair.
16
-
-`getItem(key)` -- get the value by key.
17
-
-`removeItem(key)` -- remove the key with its value.
18
-
-`clear()` -- delete everything.
19
-
-`key(index)` -- get the key on a given position.
20
-
-`length` -- the number of stored items.
15
+
-`setItem(key, value)` -- 存储键值对。
16
+
-`getItem(key)` -- 根据键名获取相应的值。
17
+
-`removeItem(key)` -- 删除该键对应的键值对。
18
+
-`clear()` -- 清空存储。
19
+
-`key(index)` -- 获取该索引下的键名。
20
+
-`length` -- 存储对象的长度。
21
21
22
-
As you can see, it's like a `Map`collection (`setItem/getItem/removeItem`), but also keeps elements order and allows to access by index with `key(index)`.
That's allowed for historical reasons, and mostly works, but generally not recommended for two reasons:
65
-
66
-
1. If the key is user-generated, it can be anything, like `length` or `toString`, or another built-in method of `localStorage`. In that case `getItem/setItem` work fine, while object-like access fails:
2. There's a `storage` event, it triggers when we modify the data. That event does not happen for object-like access. We'll see that later inthis chapter.
That's exactly because `sessionStorage` is bound not only to the origin, but also to the browser tab. For that reason, `sessionStorage` is used sparingly.
When the data gets updated in `localStorage` or `sessionStorage`, [storage](https://www.w3.org/TR/webstorage/#the-storage-event) event triggers, with properties:
Also, `event.storageArea` contains the storage object -- the event is the same for both `sessionStorage` and `localStorage`, so `storageArea` references the one that was modified. We may even want to set something back in it, to "respond" to a change.
**That allows different windows from the same origin to exchange messages.**
218
215
219
-
Modern browsers also support [Broadcast channel API](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API), the special API for same-origin inter-window communication, it's more full featured, but less supported. There are libraries that polyfill that API, based on `localStorage`, that make it available everywhere.
216
+
## 总结
220
217
221
-
## Summary
218
+
Web 存储对象 `localStorage` 和 `sessionStorage` 允许我们在浏览器中保存键值对。
222
219
223
-
Web storage objects `localStorage` and `sessionStorage` allow to store key/value in the browser.
224
-
- Both `key` and `value` must be strings.
225
-
- The limit is 2mb+, depends on the browser.
226
-
- They do not expire.
227
-
- The data is bound to the origin (domain/port/protocol).
220
+
- 所有的 `key` 和 `value` 都必须是字符串。
221
+
- 存储大小限制为 2mb+,取决于浏览器也会有所不同。
222
+
- 它们不会过期。
223
+
- 数据绑定在同一来源下(域名/端口/协议)。
228
224
229
225
|`localStorage`|`sessionStorage`|
230
226
|----------------|------------------|
231
-
|Shared between all tabs and windows with the same origin | Visible within a browser tab, including iframes from the same origin|
-`removeItem(key)`--remove the key with its value.
239
-
-`clear()`--delete everything.
240
-
-`key(index)`--get the key number `index`.
241
-
-`length`--the number of stored items.
242
-
-Use`Object.keys`to get all keys.
243
-
-We access keys as object properties, in that case `storage`event isn't triggered.
232
+
-`setItem(key, value)` -- 存储键值对。
233
+
-`getItem(key)` -- 根据键名获取相应的值。
234
+
-`removeItem(key)` -- 删除该键对应的键值对。
235
+
-`clear()` -- 清空存储。
236
+
-`key(index)` -- 获取该索引下的键名。
237
+
-`length` -- 存储对象的长度。
238
+
-使用`Object.keys`获取所有的键。
239
+
-假如我们使用对象属性的形式来访问键,则 `storage`事件不会被触发。
244
240
245
-
Storage event:
241
+
Storage 事件:
246
242
247
-
- Triggers on `setItem`, `removeItem`, `clear` calls.
248
-
- Contains all the data about the operation, the document `url` and the storage object.
249
-
- Triggers on all `window` objects that have access to the storage except the one that generated it (within a tab for `sessionStorage`, globally for `localStorage`).
0 commit comments