CookieStore: set() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Service Workers.

The set() method of the CookieStore interface sets a cookie with the given name and value or options object.

Syntax

js
set(name, value)
set(options)

Parameters

This method requires one of the following:

name Optional

A string with the name of the cookie.

value Optional

A string with the value of the cookie.

Or

options Optional

An object containing:

domain Optional

A string containing the domain of the cookie. Defaults to null.

expires Optional

A timestamp, given as Unix time in milliseconds, containing the expiration date of the cookie. Defaults to null.

name

A string with the name of a cookie.

partitioned Optional

A boolean value that defaults to false. If set to true, the set cookie will be a partitioned cookie. See Cookies Having Independent Partitioned State (CHIPS) for more information.

path Optional

A string containing the path of the cookie. Defaults to /.

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. This is the default.

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

value

A string with the value of the cookie.

Return value

A Promise that resolves with undefined when setting the cookie completes.

Exceptions

SecurityError DOMException

Thrown if the origin can not be serialized to a URL.

TypeError

Thrown if setting the cookie with the given name and value or options fails.

Examples

The following example sets a cookie by passing an object with name, value, expires, and domain.

js
const day = 24 * 60 * 60 * 1000;

cookieStore.set({
  name: "cookie1",
  value: "cookie1-value",
  expires: Date.now() + day,
  domain: "example.com",
});

Specifications

Specification
Cookie Store API
# dom-cookiestore-set

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
set
partitioned option

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support