Cookies are a powerful way to store variables between user visits. YUI has a set of static functions that simplifies the reading and writing of cookies. Additionally, you can use it to write fewer cookies by storing multiple values in a single cookie. This recipe will explain how to use the static functions of the Cookie component, available on the YAHOO.util.Cookie
namespace.
The simplest way to add a cookie that expires at the end of the current user session is:
YAHOO.util.Cookie.set('testCookie1', 'testValue');
You can provide a third optional object literal argument to define any of these additional cookie properties:
YAHOO.util.Cookie.set('testCookie2', 'testValue', { expires: new Date("January 1, 2012"), // last until date domain: 'myserver.com', // bind to your server path: '/', // bind to a path on the server secure: true // use SSL });
Use the following to read a cookie:
var value = YAHOO.util.Cookie.get("testCookie1");
To convert the cookie to another basic data type, such as a Number
, use:
var value = YAHOO.util.Cookie.get("aNumberCookie", Number);
To convert the cookie to another data type manually, use:
var value = YAHOO.util.Cookie.get("aHexCookie", function(stringValue) { return parseInt(stringValue, 16); // hexadecimal value });
When deleting a cookie you must specify the same options as were used when creating the cookie (except expires). Here is how to delete the two cookies we previously created:
YAHOO.util.Cookie.remove('testCookie'); YAHOO.util.Cookie.remove('testCookie2', { domain: 'myserver.com', path:'/', secure: true });
To check for the existence of a cookie, use:
var cookieName = 'testCookie1'; if (YAHOO.util.Cookie.exists(cookieName)) { alert('Cookie "' + cookieName + '" exists'); }
To reduce the number of cookies used by your site, the YUI Cookie component provides a subcookie framework, which stores multiple pieces of data into a single cookie. First create the configuration options that will be used for the subcookies:
var cookieOptions = { domain: "yourserver.com" }; var cookieName = 'testCookie';
To set a subcookie use:
var subCookieName = 'count'; var subCookieValue = 22; YAHOO.util.Cookie.setSub( cookieName, subCookieName, subCookieValue, cookieOptions);
To set many subcookies at one time use:
var testCookiesObject = { count: 22, name: 'matt', isUser: true }; YAHOO.util.Cookie.setSubs( cookieName, testCookiesObject, cookieOptions);
To fetch a subcookie, use:
var value = YAHOO.util.Cookie.getSub(cookieName, 'count');
You may also convert subcookies to non-string datatypes by passing a third argument:
var value = YAHOO.util.Cookie.getSub( cookieName, 'count', Number);
To retrieve all subcookies as an object, use:
var oData = YAHOO.util.Cookie.getSubs(cookieName);
And to remove a subcookie use:
YAHOO.util.Cookie.removeSub( cookieName, "count", cookieOptions);
The cookie
property of document
is where browsers store cookies for the JavaScript engine. The YUI Cookie component reads and writes to this value in a simple and concise way, so you don't have to. It is important to remember when creating cookies that if you do not specify the expires
property, then the cookie will expire at the end of the user's session. Also, when deleting cookies, the same options that were used to create the cookie should be passed to the remove
function or the cookie will not be removed.
Subcookies are cookies containing a collection of other name/value pairs. This is accomplished by using a URL parameter query like structure:
cookiename=subcookiename1=value1&subcookiename2=value2&subcookiename3=value3
When a cookie is converted into a subcookie, any existing value will be overwritten by the subcookies infrastructure.
To learn more about cookies and see additional examples, visit http://developer.yahoo.com/yui/cookie/.