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/01-cookie/article.md
+19-16
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,7 @@ We leave it as an exercise for the reader. Also, at the end of the chapter, you'
41
41
42
42
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.
43
43
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.**
45
45
46
46
For instance, this call sets a cookie with the name `user` and value `John`:
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
+
```
133
126
134
127
To summarize, the `domain` option allows to make a cookie accessible at subdomains.
135
128
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
+
136
139
## expires, max-age
137
140
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"
139
142
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.
141
144
142
145
-**`expires=Tue, 19 Jan 2038 03:14:07 GMT`**
143
146
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).
145
148
146
149
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:
0 commit comments