Skip to content

Commit 502fde3

Browse files
authored
Resolve conflicts + Update article
1 parent e2814bb commit 502fde3

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

6-data-storage/02-localstorage/article.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ What's interesting about them is that the data survives a page refresh (for `ses
77
We already have cookies. Why additional objects?
88

99
- 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.
1111
- 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.
1212

1313
Both storage objects provide same methods and properties:
@@ -19,11 +19,8 @@ Both storage objects provide same methods and properties:
1919
- `key(index)` -- get the key on a given position.
2020
- `length` -- the number of stored items.
2121

22-
<<<<<<< HEAD
23-
=======
2422
As you can see, it's like a `Map` collection (`setItem/getItem/removeItem`), but also allows access by index with `key(index)`.
2523

26-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
2724
Let's see how it works.
2825

2926
## localStorage demo
@@ -45,9 +42,9 @@ localStorage.setItem('test', 1);
4542
alert( localStorage.getItem('test') ); // 1
4643
```
4744

48-
We only have to be on the same domain/port/protocol, the url path can be different.
45+
We only have to be on the same origin (domain/port/protocol), the url path can be different.
4946

50-
The `localStorage` is shared, so if we set the data in one window, the change becomes visible in the other one.
47+
The `localStorage` is shared between all windows with the same origin, so if we set the data in one window, the change becomes visible in another one.
5148

5249
## Object-like access
5350

@@ -64,7 +61,7 @@ alert( localStorage.test ); // 2
6461
delete localStorage.test;
6562
```
6663

67-
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:
6865

6966
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:
7067
```js run
@@ -76,11 +73,11 @@ That's allowed for historical reasons, and mostly works, but generally not recom
7673

7774
## Looping over keys
7875

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?
8077
8178
Unfortunately, storage objects are not iterable.
8279
83-
One way is to use "array-like" iteration:
80+
One way is to loop over them as over an array:
8481
8582
```js run
8683
for(let i=0; i<localStorage.length; i++) {
@@ -89,9 +86,9 @@ for(let i=0; i<localStorage.length; i++) {
8986
}
9087
```
9188
92-
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.
9390
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:
9592

9693
```js run
9794
// bad try
@@ -127,7 +124,7 @@ The latter works, because `Object.keys` only returns the keys that belong to the
127124

128125
Please note that both key and value must be strings.
129126

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:
131128

132129
```js run
133130
sessionStorage.user = {name: "John"};
@@ -160,7 +157,7 @@ Properties and methods are the same, but it's much more limited:
160157
161158
- The `sessionStorage` exists only within the current browser tab.
162159
- 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).
164161
- The data survives page refresh, but not closing/opening the tab.
165162
166163
Let's see that in action.
@@ -185,7 +182,7 @@ That's exactly because `sessionStorage` is bound not only to the origin, but als
185182
186183
When the data gets updated in `localStorage` or `sessionStorage`, [storage](https://www.w3.org/TR/webstorage/#the-storage-event) event triggers, with properties:
187184
188-
- `key` – the key that was changed (null if `.clear()` is called).
185+
- `key` – the key that was changed (`null` if `.clear()` is called).
189186
- `oldValue` – the old value (`null` if the key is newly added).
190187
- `newValue` – the new value (`null` if the key is removed).
191188
- `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
201198
You might want to open this page in two browser windows to test the code below.
202199
```
203200

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.
205202

206203
```js run
207204
// triggers on updates made to the same storage from other documents
@@ -215,7 +212,7 @@ localStorage.setItem('now', Date.now());
215212

216213
Please note that the event also contains: `event.url` -- the url of the document where the data was updated.
217214

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.
219216

220217
**That allows different windows from the same origin to exchange messages.**
221218

@@ -232,21 +229,21 @@ Web storage objects `localStorage` and `sessionStorage` allow to store key/value
232229
| `localStorage` | `sessionStorage` |
233230
|----------------|------------------|
234231
| Shared between all tabs and windows with the same origin | Visible within a browser tab, including iframes from the same origin |
235-
| Survives browser restart | Dies on tab close |
232+
| Survives browser restart | Survives page refresh (but not tab close) |
236233
237234
API:
238235
239236
- `setItem(key, value)` -- store key/value pair.
240237
- `getItem(key)` -- get the value by key.
241238
- `removeItem(key)` -- remove the key with its value.
242239
- `clear()` -- delete everything.
243-
- `key(index)` -- get the key on a given position.
240+
- `key(index)` -- get the key number `index`.
244241
- `length` -- the number of stored items.
245242
- Use `Object.keys` to get all keys.
246-
- Can use the keys as object properties, in that case `storage` event doesn't trigger.
243+
- We access keys as object properties, in that case `storage` event isn't triggered.
247244

248245
Storage event:
249246

250247
- 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`.
252249
- 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

Comments
 (0)