Cookie

Cookie is an object oriented wrapper around the native cookie support of a browser.

You can work with the Cookie object at class level.

Cookie.set('name', 'value');
Cookie.set('name', 'value', {duration: 10});

Cookie.get('name');

Cookie.remove('name');

Or you can use instances like this:

var cookie = new Cookie('name');
var cookie = new Cookie('name', {duration: 4});

cookie.set('value');

cookie.remove();

Options

You can use the following options with your cookies:

NameDuration
durationnumber of days the cookie will stay alive
domaina string domain name
patha string path
secureboolean to set if the cookie should get marked as secure

Values Escaping

You can safely feed the API with any strings, all the values will be automatically encoded and decoded.

Cookie.set('name', '%"&=*"');

Cookie.get('name'); // -> '%"&=*"'

And if you include the JSON feature on your page, you will be able to save/retrieve any JSON exportable objects, like booleans, arrays or plain objects.

Methods

get, remove, set, initialize, get, remove, set

top ↑get

Cookie.get(String name) -> String value or null

Description

Finds and reads a cookie by name. Returns null if nothing is found.

Example

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

Cookie.get('name'); // -> 'value'

top ↑remove

Cookie.remove(String name) -> Cookie

Description

Erases a cookie value by name.

Example

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

Cookie.remove('name');

Cookie.get('name'); // -> null

top ↑set

Cookie.set(String name, String value[, Object options]) -> Cookie

Description

Sets the cookie.

Example

Cookie.set('name', 'value');
Cookie.set('name', 'value', {duration: 4});

top ↑initialize

initialize(String name[, Object options])

Description

Basic constructor. See the list of options in the description above.

Example

var cookie = new Cookie('name');
var cookie = new Cookie('name', {duration: 2});

top ↑get

get() -> String value or null

Description

Reads the cookie value.

Example

var cookie = new Cookie('name');

cookie.set('value');

cookie.get(); // -> 'value'

top ↑remove

remove() -> Cookie self

Description

Removes the cookie from of the browser memory.

Example

var cookie = new Cookie('name');
cookie.set('value');
cookie.get(); // -> 'value'

cookie.remove();

cookie.get(); // -> null

top ↑set

set(String value) -> Cookie self

Description

Sets the cookie value.

Example

var cookie = new Cookie('name');

cookie.set('value');

cookie.get(); // -> 'value'