back to Overview

$.setCookie()

Creates, updates or deletes a cookie.
Complete distribution only, not available in stand-alone modules.

Syntax Variants

$.setCookie(name, value)
$.setCookie(name, value, dateOrDays)

Parameters

name
the name of the cookie. This should ideally be an alphanumeric name, as it will not be escaped by Minified and this guarantees compatibility with all systems. If it contains a '=', it is guaranteed not to work, because it breaks the cookie syntax.
value
the value of the cookie. All characters can be used. Non-Alphanumeric other than "*@-_+./" will be escaped using the JavaScript escape() function, unless you set the optional dontEscape parameter.
dateOrDays (optional)
specifies when the cookie expires. Can be either a Date object or a number that specifies the amount of days. If not set, the cookie has a session lifetime, which means it will be deleted as soon as the browser has been closed. If the number negative or the date in the past, the cookie will be deleted.
dontEscape (optional)
if set, the cookie value is not escaped. Note that without escaping you can not use every possible character (e.g. ";" will break the cookie), but it may be needed for interoperability with systems that need some non-alphanumeric characters unescaped or use a different escaping algorithm.

Description

Creates, updates or deletes a cookie. If there is an an existing cookie of the same name, will be overwritten with the new value and settings.

To delete a cookie, overwrite it with an expiration date in the past. The easiest way to do this is to use -1 as third argument.

Example

Reads the existing cookie 'numberOfVisits', increases the number and stores it:

var visits = $.getCookie('numberOfVisits');
$.setCookie('numberOfVisits',
                     visits ? (parseInt(visits) + 1) : 1,   // if cookie not set, start with 1
                     365);                                  // store for 365 days

Example

Deletes the cookie 'numberOfVisits':

$.setCookie('numberOfVisits', '', -1);

See also..

Comments

comments powered by Disqus

back to Overview

Functions