CookieStore
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 CookieStore
interface of the Cookie Store API provides methods for getting and setting cookies asynchronously from either a page or a service worker.
The CookieStore
is accessed via attributes in the global scope in a Window
or ServiceWorkerGlobalScope
context. Therefore there is no constructor.
Instance methods
-
The
delete()
method deletes a cookie with the givenname
oroptions
object, it returns aPromise
that resolves when the deletion completes. -
The
get()
method gets a single cookie with the givenname
oroptions
object, it returns aPromise
that resolves with details of a single cookie. -
The
getAll()
method gets all matching cookies, it returns aPromise
that resolves with a list of cookies. -
The
set()
method sets a cookie with the givenname
andvalue
oroptions
object, it returns aPromise
that resolves when the cookie is set.
Events
change
-
The
change
event fires when a change is made to any cookie.
Examples
In this example, we set a cookie and write to the console feedback as to whether the operation succeeded or failed.
const day = 24 * 60 * 60 * 1000;
cookieStore
.set({
name: "cookie1",
value: "cookie1-value",
expires: Date.now() + day,
domain: "example.com",
})
.then(
() => {
console.log("It worked!");
},
(reason) => {
console.error("It failed: ", reason);
},
);
Specifications
Specification |
---|
Cookie Store API # CookieStore |
Browser compatibility
BCD tables only load in the browser