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
Copy file name to clipboardExpand all lines: 6-data-storage/02-localstorage/article.md
+17-20Lines changed: 17 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ What's interesting about them is that the data survives a page refresh (for `ses
7
7
We already have cookies. Why additional objects?
8
8
9
9
- 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
-
-The server can't manipulate storage objects via HTTP headers, everything's done in JavaScript.
10
+
-Also unlike cookies, the server can't manipulate storage objects via HTTP headers. Everything's done in JavaScript.
11
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.
12
12
13
13
Both storage objects provide same methods and properties:
@@ -19,11 +19,8 @@ Both storage objects provide same methods and properties:
19
19
-`key(index)` -- get the key on a given position.
20
20
-`length` -- the number of stored items.
21
21
22
-
<<<<<<< HEAD
23
-
=======
24
22
As you can see, it's like a `Map` collection (`setItem/getItem/removeItem`), but also allows access by index with `key(index)`.
That's allowed for historical reasons, and mostly works, but generally not recommended for two reasons:
64
+
That's allowed for historical reasons, and mostly works, but generally not recommended, because:
68
65
69
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:
70
67
```js run
@@ -76,11 +73,11 @@ That's allowed for historical reasons, and mostly works, but generally not recom
76
73
77
74
## Looping over keys
78
75
79
-
Methods provide get/set/remove functionality. But how to get all the keys?
76
+
As we've seen, the methods provide "get/set/remove by key" functionality. But how to get all saved values or keys?
Another way is to use object-specific `for key in localStorage` loop.
89
+
Another way is to use `for key in localStorage` loop, just as we do with regular objects.
93
90
94
-
That iterates over keys, but also outputs few built-in fields that we don't need:
91
+
It iterates over keys, but also outputs few built-in fields that we don't need:
95
92
96
93
```js run
97
94
// bad try
@@ -127,7 +124,7 @@ The latter works, because `Object.keys` only returns the keys that belong to the
127
124
128
125
Please note that both key and value must be strings.
129
126
130
-
If we any other type, like a number, or an object, it gets converted to string automatically:
127
+
If were any other type, like a number, or an object, it gets converted to string automatically:
131
128
132
129
```js run
133
130
sessionStorage.user = {name: "John"};
@@ -160,7 +157,7 @@ Properties and methods are the same, but it's much more limited:
160
157
161
158
- The `sessionStorage` exists only within the current browser tab.
162
159
- Another tab with the same page will have a different storage.
163
-
- But it is shared between iframes in the tab (assuming they come from the same origin).
160
+
- But it is shared between iframes in the same tab (assuming they come from the same origin).
164
161
- The data survives page refresh, but not closing/opening the tab.
165
162
166
163
Let's see that in action.
@@ -185,7 +182,7 @@ That's exactly because `sessionStorage` is bound not only to the origin, but als
185
182
186
183
When the data gets updated in `localStorage` or `sessionStorage`, [storage](https://www.w3.org/TR/webstorage/#the-storage-event) event triggers, with properties:
187
184
188
-
-`key` – the key that was changed (nullif`.clear()` is called).
185
+
- `key` – the key that was changed (`null` if `.clear()` is called).
189
186
- `oldValue` – the old value (`null` if the key is newly added).
190
187
- `newValue` – the new value (`null` if the key is removed).
191
188
- `url` – the url of the document where the update happened.
@@ -201,7 +198,7 @@ Imagine, you have two windows with the same site in each. So `localStorage` is s
201
198
You might want to open this page in two browser windows to test the code below.
202
199
```
203
200
204
-
Now if both windows are listening for `window.onstorage`, then each one will react on updates that happened in the other one.
201
+
If both windows are listening for`window.onstorage`, then each one will react on updates that happened in the other one.
205
202
206
203
```js run
207
204
// triggers on updates made to the same storage from other documents
Please note that the event also contains:`event.url`-- the url of the document where the data was updated.
217
214
218
-
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 event want to set something back in it, to "respond" to a change.
215
+
Also, `event.storageArea` contains the storage object -- the event is the same for both `sessionStorage` and `localStorage`, so `event.storageArea` references the one that was modified. We may even want to set something back in it, to "respond" to a change.
219
216
220
217
**That allows different windows from the same origin to exchange messages.**
221
218
@@ -232,21 +229,21 @@ Web storage objects `localStorage` and `sessionStorage` allow to store key/value
232
229
| `localStorage` | `sessionStorage` |
233
230
|----------------|------------------|
234
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.
242
239
- `clear()` -- delete everything.
243
-
-`key(index)`-- get the key on a given position.
240
+
- `key(index)` -- get the key number `index`.
244
241
- `length` -- the number of stored items.
245
242
- Use `Object.keys` to get all keys.
246
-
-Can use the keys as object properties, in that case `storage`eventdoesn't trigger.
243
+
- We access keys as object properties, in that case `storage` event isn't triggered.
247
244
248
245
Storageevent:
249
246
250
247
- Triggers on `setItem`, `removeItem`, `clear` calls.
251
-
- Contains all the data about the operation, the document `url` and the storage object.
248
+
- Contains all the data about the operation (`key/oldValue/newValue`), the document`url` and the storage object`storageArea`.
252
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