This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

cookie

Summary

Sets or gets the string value of a cookie.

Property of dom/Documentdom/Document

Syntax

var cookies = document.cookie;
document.cookie = newCookie;

Return Value

Returns an object of type StringString

The current cookies of the document.

Examples

//set a cookie with a name, a value, and a number of days before expiring
function setCookie(aname, avalue, expdays) {
    var dt = new Date();
    dt.setTime(dt.getTime() + (expdays*24*60*60*1000));
    var expires = "expires=" + dt.toGMTString();
    document.cookie = aname + "=" + avalue + "; " + expires;
}

//retrieve all document cookies; if passed cookie name found, return value
//if passed cookie name not found, return null
function getCookie(aname) {
    var name = aname + "=";
    var carray = document.cookie.split(';');
    for(var i = 0; i < carray.length; i++) {
        var cn = carray[i];
        if (cn.indexOf(name) == 0) return cn.substring(name.length,cn.length);
    }
    return "";
}

Usage

 A cookie is a small piece of information. Each cookie is stored in a name=value pair called a crumb—that is, if the cookie name is "id" and you want to save the id value as "this," the cookie is saved as id=this. You can store up to a maxium of 50 name=value pairs in a cookie; the cookie is always returned as a string of all the cookies that apply to the document. This means that you must parse the string returned to find the values of individual cookies.

Cookies accumulate each time the property is set. Once the maximum pair limit is reached, subsequent set will push older name=value pair off in favor of the new name=value pair. You can use the split method to extract a value stored in a cookie.

Related specifications

DOM Level 2 HTML
Recommendation
WHATWG HTML
Living Standard
W3C HTML5
Working Draft

Attributions