Skip to content

Commit 3e92613

Browse files
authoredFeb 11, 2024
Improve options description
1 parent daca277 commit 3e92613

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed
 

‎6-data-storage/01-cookie/article.md

+19-16
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ We leave it as an exercise for the reader. Also, at the end of the chapter, you'
4141

4242
We can write to `document.cookie`. But it's not a data property, it's an [accessor (getter/setter)](info:property-accessors). An assignment to it is treated specially.
4343

44-
**A write operation to `document.cookie` updates only cookies mentioned in it, but doesn't touch other cookies.**
44+
**A write operation to `document.cookie` updates only the cookie mentioned in it and doesn't touch other cookies.**
4545

4646
For instance, this call sets a cookie with the name `user` and value `John`:
4747

@@ -68,6 +68,7 @@ alert(document.cookie); // ...; my%20name=John%20Smith
6868

6969
```warn header="Limitations"
7070
There are a few limitations:
71+
- You can only set/update a single cookie at a time using `document.cookie`.
7172
- The `name=value` pair, after `encodeURIComponent`, should not exceed 4KB. So we can't store anything huge in a cookie.
7273
- The total number of cookies per domain is limited to around 20+, the exact limit depends on the browser.
7374
```
@@ -80,16 +81,6 @@ The options are listed after `key=value`, delimited by `;`, like this:
8081
document.cookie = "user=John; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT"
8182
```
8283

83-
## path
84-
85-
- **`path=/mypath`**
86-
87-
The url path prefix must be absolute. It makes the cookie accessible for pages under that path. By default, it's the current path.
88-
89-
If a cookie is set with `path=/admin`, it's visible on pages `/admin` and `/admin/something`, but not at `/home` or `/adminpage`.
90-
91-
Usually, we should set `path` to the root: `path=/` to make the cookie accessible from all website pages.
92-
9384
## domain
9485

9586
- **`domain=site.com`**
@@ -102,7 +93,7 @@ It's a safety restriction, to allow us to store sensitive data in cookies that s
10293

10394
By default, a cookie is accessible only at the domain that set it.
10495

105-
Please note, by default, a cookie is also not shared to a subdomain as well, such as `forum.site.com`.
96+
Please note, by default, a cookie is not shared with a subdomain, such as `forum.site.com`.
10697

10798
```js
10899
// if we set a cookie at site.com website...
@@ -129,19 +120,31 @@ document.cookie = "user=John; *!*domain=site.com*/!*"
129120
alert(document.cookie); // has cookie user=John
130121
```
131122

132-
For historical reasons, `domain=.site.com` (with a dot before `site.com`) also works the same way, allowing access to the cookie from subdomains. That's an old notation and should be used if we need to support very old browsers.
123+
```warn header="Legacy syntax"
124+
Historically, `domain=.site.com` (with a dot before `site.com`) used to work the same way, allowing access to the cookie from subdomains. Leading dots in domain names are now ignored, but some browsers may decline to set the cookie containing such dots.
125+
```
133126

134127
To summarize, the `domain` option allows to make a cookie accessible at subdomains.
135128

129+
## path
130+
131+
- **`path=/mypath`**
132+
133+
The url path prefix must be absolute. It makes the cookie accessible for pages under that path. By default, it's the current path.
134+
135+
If a cookie is set with `path=/admin`, it's visible on pages `/admin` and `/admin/something`, but not at `/home`, `/home/admin` or `/`.
136+
137+
Usually, we should set `path` to the root: `path=/` to make the cookie accessible from all website pages. If this option is not set the default is calculated using [this method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#path_default_value).
138+
136139
## expires, max-age
137140

138-
By default, if a cookie doesn't have one of these options, it disappears when the browser is closed. Such cookies are called "session cookies"
141+
By default, if a cookie doesn't have one of these options, it disappears when the browser/tab is closed. Such cookies are called "session cookies"
139142

140-
To let cookies survive a browser close, we can set either the `expires` or `max-age` option.
143+
To let cookies survive a browser close, we can set either the `expires` or `max-age` option. `max-Age` has precedence if both are set.
141144

142145
- **`expires=Tue, 19 Jan 2038 03:14:07 GMT`**
143146

144-
The cookie expiration date defines the time when the browser will automatically delete it.
147+
The cookie expiration date defines the time when the browser will automatically delete it (according to the browser's time zone).
145148

146149
The date must be exactly in this format, in the GMT timezone. We can use `date.toUTCString` to get it. For instance, we can set the cookie to expire in 1 day:
147150

0 commit comments

Comments
 (0)
Failed to load comments.