This block provides an object with a set of methods for working with browser cookies (the JS document.cookie property).
| Name | Return type | Description |
|---|---|---|
get(name) |
String | null |
Gets the value stored in a browser cookie. |
set(name, val, [options]) |
String |
Sets the cookie with the specified name. |
The block is implemented in:
jsget methodUse this method to get the value stored in a cookie for the name passed in the argument.
Accepted arguments:
| Argument | Type | Description |
|---|---|---|
name* |
String |
The name of the cookie. |
* Required argument.
Returns:
String — If a cookie with the specified name was set. The value is automatically decoded using decodeURIComponent.null — If a cookie with the specified name doesn't exist.Example:
modules.require('cookie', function(cookie) {
cookie.set('mycookie', 'foobar');
console.log(cookie.get('mycookie')); // 'foobar'
console.log(cookie.get('foo')); // null
});
set methodUse this method to set the cookie with the specified name. In addition to the name and value, you can pass the method a hash with additional cookie parameters.
Accepted arguments:
| Argument | Type | Description |
|---|---|---|
name* |
String |
The name of the cookie. |
val* |
String | null |
The value of the cookie. If the value is set to null, the cookie is deleted. |
[options] |
Object |
Options. Object properties • expires (Number) – The cookie's time to live, in days. If the value is negative, the cookie is deleted. Alternatively, you can pass a generated date object (new Date()) for the value. • path (String) – The path from the domain root where the cookie will be available. • domain (String) – The domain. By default, this is the current domain. • secure (Boolean) – Flag indicating that an encrypted SSL connection must be used with the cookie. By default, it is false. |
* Required argument.
Returns: the this object.
Example:
modules.require('cookie', function(cookie) {
cookie.set('mycookie', 'foobar', {
expires : 1, // lifetime is one day
path : '/', // available for all pages secure
secure : true // only send the cookie over SSL
});
console.log(cookie.get('mycookie')); // 'foobar'
cookie.set('mycookie', null); // deleting the cookie
console.log(cookie.get('mycookie')); // null
});