CookieStore

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

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.

Properties

Event handlers

CookieStore.onChange

The onchange EventHandler is called whenever a cookie is changed.

Methods

CookieStore.delete()

The delete() method deletes a cookie with the given name or options object, it returns a Promise that resolves when the deletion completes.

CookieStore.get()

The get() method gets a single cookie with the given name or options object, it returns a Promise that resolves with details of a single cookie.

CookieStore.getAll()

The getAll() method gets all matching cookies, it returns a Promise that resolves with a list of cookies.

CookieStore.set()

The set() method sets a cookie with the given name and value or options object, it returns a Promise that resolves when the cookie is set.

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(
  function() {
    console.log("It worked!");
  },
  function(reason) {
    console.error("It failed: ", reason);
  }
);

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
CookieStore
87
87
No
No
73
No
87
87
No
62
No
14.0
delete
87
87
No
No
73
No
87
87
No
62
No
14.0
get
87
87
No
No
73
No
87
87
No
62
No
14.0
getAll
87
87
No
No
73
No
87
87
No
62
No
14.0
onchange
87
87
No
No
73
No
87
87
No
62
No
14.0
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