Link Search Menu Expand Document

set( key : string, value : any, ?options : object )

Parameters

  • key String - The key of the cookie to storage the value. Only letters, numbers, dashes - and underscores _ are allowed.
  • value Any - The value of the cookie. Only JSON-Valid values or Javascript Dates are valid to save in the cookies storage.
  • ?options cookie_options - (Optional) Set cookie options.
  • ?expires Number/Date/ISOString/UTCString - (Optional) Instead of options parameter to set a expiration date. If you set a number, the parameter will be interpreted as days. Default: 30 Days

Returns

  • cookie OreoCookie - A object with all cookie information.

Example

oreo.set( 'cookie_key', 'cookie_value' ) ;

/* Fast-Line Version [v] */
var cookie = oreo( 'my-cookie', true ) ;

/* Date example [v] */
var cookie = oreo( 'date-cookie', new Date( ) ) ;

/* Expiration examples [v] */
/* will expire in 8 days [v] */
var cookie = oreo( 'my-cookie', 'value', 8 ) ;
/* will expire in a year [v] */
expiration = new Date( ) ;
expiration = expiration.setFullYear( expiration.getFullYear( ) + 1 ) ;
var cookie = oreo( 'my-cookie', 'value', { expires : expiration } ) ;

/* Object example [v] */
var cookie = oreo( 'json-cookie', {
  prop1 : 'value1' ,
  prop2 : 'value2'
} ) ;

Limits

By default, the package is configured to emulate browsers cookies limit (useful if you are trying to make a cross-platform app). But you can disable this limits by configuring oreo.js.

Max cookies Max cookie size
128 4096 bytes

cookie_options

An object with all optinal options to control the cookie behavior. Basically you can set any HTTP cookie options because the cookie will be stored as string like a typical cookie, but only a few settings will be useful for non-browser implementations (Electron, Cordova and Backend-Servers).

The next defined options are available for all platforms. To check all other cookie options for browsers, just check this mozilla article.

  • cookie_options Object
    • expires Number/Date/ISOString/UTCString - Set a expiration date for the cookie. Default: 30 Days
    • path String - Set the path access to the cookie. See path access article to understand how cookie access works depending on the current web-app page.

Example

cookie.set( 'my-cookie', 'value', {
  path : '/games/awesome-game/' , // <= This cookie will only available if the user is under this path even all files iniside the defined path.
  samesite : 'Lax' // <= Only for browsers.
} ) ;