Js-cookie: Set default settings?

Created on 14 May 2018  路  6Comments  路  Source: js-cookie/js-cookie

Its possible to set a domain string as default setting?

Actual for each set/get

Cookies.set('name', 'value', { expires: 1, domain: '.example.com' }));

Maybe better and shorter

// Set default values
Cookies.settings({ expires: 1, domain:  '.example.com' });
Cookies.set('name', 'value')); // => expires is 1 day; domain is '.example.com'
Cookies.set('name', 'value', { expires: 3 })); // => domain is '.example.com'

In most cases I think the domain is always same.

Most helpful comment

Oh, thanks! It would help much to include a simple code example. :)

All 6 comments

From the readme:

Cookie attributes defaults can be set globally by setting properties of the Cookies.defaults object

https://github.com/js-cookie/js-cookie/blob/master/README.md

(We might have to make this sentence more prominent.)

Oh, thanks! It would help much to include a simple code example. :)

Agreed, this could use an example.

My code is:
Cookies.defaults({ secure: true, sameSite: 'lax' });
but it gives:
TypeError: Cookies.defaults is not a function

How can I solve?

@Fabio-Zeus-Soft

With v2: Cookies.defaults = { ... }

Configure Default Cookie Attributes

Cookies.defaults = {
  path: "/",
  domain: ".example.com",
  secure: false,
  expires: 365,
};

Create a New Cookie with Default Values

Cookies.set('name', 'value');

Set Cookie and Override Default(s)

Cookies.set('name', 'value', { expires: 7, path: '' });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

oninross picture oninross  路  9Comments

hrahimi270 picture hrahimi270  路  6Comments

migsyboy picture migsyboy  路  7Comments

luis-pato picture luis-pato  路  6Comments

jiangdefu picture jiangdefu  路  3Comments