JavaScript can access and modify HTTP cookies through the document.cookie
interface. Unfortunately it is a bit limited in accessing individual cookies. Cookie is a wrapper around the native interface to help with this.
When setting a cookie, an optional params object can be specified of the form:
{
'domain': '.domain.com',
'path': '/',
'expires': 'Wdy, DD-Mon-YY HH:MM:SS GMT',
'max_age': DELTA_SECONDS,
'secure': TRUTHY
}
When getting the cookie value, an array is returned with any matching cookies.
RFC2109 states cookies are unique based on their name, domain and path. The document.cookie
interface returns all accessible cookies (name, value pairs) by the domain and path matching rules. This creates a situation where a cookie name could be shared between multiple cookies.
When setting a value, care should be taken to assign the correct domain and path to avoid either creating a new cookie or updating the incorrect one.
Using Cookie as an object:
Cookie.get('cookieName');
Cookie.set('cookieName', 'cookieValue', OPTIONAL_PARAMS_OBJECT);
Cookie.clear('cookieName');
Using Cookie as a constructor:
var c = new Cookie('cookieName');
c.get();
c.set('cookieValue', OPTIONAL_PARAMS_OBJECT);
c.clear();