Frequently Asked Questions

Fagner Brack edited this page Dec 28, 2018 · 8 revisions

Reference version: 2.0.4

How to make the cookie expire in less than a day?

JavaScript Cookie supports a Date instance to be passed in the expires attribute. That provides a lot of flexibility since a Date instance can specify any moment in time.

Take for example, when you want the cookie to expire 15 minutes from now:

var inFifteenMinutes = new Date(new Date().getTime() + 15 * 60 * 1000);
Cookies.set('foo', 'bar', {
    expires: inFifteenMinutes
});

Also, you can specify fractions to expire in half a day (12 hours):

var inHalfADay = 0.5;
Cookies.set('foo', 'bar', {
    expires: inHalfADay
});

Or in 30 minutes:

var in30Minutes = 1/48;
Cookies.set('foo', 'bar', {
    expires: in30Minutes
});

How to remove all cookies?

It's not possible to remove literally all cookies from the user's browser. Some of them might have been created with a separate domain or path attribute than the one the user is currently in or even created with the httpOnly attribute and therefore they might not be visible.

What it's possible is to try to remove all cookies that are visible to the user:

Object.keys(Cookies.get()).forEach(function(cookieName) {
  var neededAttributes = {
    // Here you pass the same attributes that were used when the cookie was created
    // and are required when removing the cookie
  };
  Cookies.remove(cookieName, neededAttributes);
});

Why are my cookies being deleted?

They are probably too big or there are too many cookies in the same domain.

According to RFC 6265, which is the specification browsers implement for cookies:

Practical user agent implementations have limits on the number and size of cookies that they can store. General-use user agents SHOULD provide each of the following minimum capabilities:

  • At least 4096 bytes per cookie (as measured by the sum of the length of the cookie's name, value, and attributes).
  • At least 50 cookies per domain.
  • At least 3000 cookies total.

This means that the browser could be truncating the cookies due to this size limit.

We recommend to not exceed the imposed limits stated in the RFC.

How to extend or add a new plugin or functionality to the library?

js-cookie does not have an easy way to add plugins or to extend any of its functionality. And that's by design.

See this for additional information and why.

Can I set two cookies with the same name?

No. js-cookie does not support two cookies with the same name in the same domain. Unexpected things might happen, see here for details.

Why the browser does not remove the cookies after I close it?

If you're using Chrome, check the feature "Continue where I left off". For more details, check here.

You can’t perform that action at this time.
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.
Press h to open a hovercard with more details.