Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The set()
method of the CookieStore
interface sets a cookie with the given name and value or options object. (See below.)
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The set()
method of the CookieStore
interface sets a cookie with the given name and value or options object. (See below.)
set(name, value); set(options);
This method requires one of the following:
name
A string with the name of the cookie.
value
A string with the value of the cookie.
An object containing:
name
A string with the name of a cookie.
value
A string with the value of the cookie.
expires
Optional
A DOMTimeStamp
containing the expiration date of the cookie.
domain
Optional
A string containing the domain of the cookie.
path
Optional
A string containing the path of the cookie.
sameSite
Optional
One of the following SameSite values:
"strict"
Cookies will only be sent in a first-party context and not be sent along with requests initiated by third party websites.
"lax"
Cookies are not sent on normal cross-site subrequests (for example to load images or frames into a third party site), but are sent when a user is navigating to the origin site (i.e. when following a link).
"none"
Cookies will be sent in all contexts.
Note: For more information on SameSite cookies see SameSite cookies explained.
TypeError
Thrown if setting the cookie with the given values fails.
DOMException
SecurityError
Thrown if the origin does not serialize to a URL.
The following example sets a cookie by passing an object with name
, value
, expires
, and domain
.
const day = 24 * 60 * 60 * 1000; cookieStore.set({ name: "cookie1", value: "cookie1-value", expires: Date.now() + day, domain: "example.com" });
Specification |
---|
Cookie Store API # dom-cookiestore-set |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
set |
87
|
87
|
No
|
No
|
73
|
No
|
87
|
87
|
No
|
62
|
No
|
14.0
|
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/set