Forum

Methodology

Toolbox

Platform

Community

desktop
touch-pad
touch-phone

cookie

This block provides an object with a set of methods for working with browser cookies (the JS document.cookie property).

Overview

Object properties and methods

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.

Public block technologies

The block is implemented in:

  • js

Description

Object properties and methods

get method

Use 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 method

Use 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

});
Object methods:
get
set

Module cookie

Inspired from $.cookie plugin by Klaus Hartl (stilbuero.de)

Object methods:

get(name):String,null
description
Returns cookie by given name
parameters
name
String
set(name, val, options):cookie
description
Sets cookie by given name
parameters
name
String
val
String
options
Object