All Snippets
Top 100 Snippets

By Language

GBIC >> Source Code >> JavaScript >> Snippet

33 Cookies

Cookies are simply text strings that a browser writes to your PC.  JavaScript can
direct the browser to write cookies.

Each web page can have 1 cookie, which can be about 4096 bytes of data.
A browser will store about 300 cookies total, with no more than 20 cookies allowed
per web site.  These values can vary slightly, depending
on the browser.  

When a cookie is written that exceeds the limits, the least recently used cookie
is deleted to make room for newer cookies.

The simplest cookie takes the form of "name=value;"

A cookie has four optional attributes :
- path identifies path to documents which may access the cookie
- domain server(s) which may access the cookie
- expires date cookie will be deleted
- secure limits cookie to transmission only over a secure channel (HTTPS)

A complete cookie looks like this :
name = value;path =/ myfolder;domain = www.domain.com;expires = date;secure = true

Notice there are no quotes or space or punctuation marks in the cookie string.

By default a cookie lasts only during the current browser session.  When the
browser session is over the cookie is destroyed unless the expires attribute
is set to a future date (expiration date).

Only when the current browser session is over is the cookie written to a file.

To get the entire cookie content use :
var CookieString = document.cookie;

To set the cookie use :
document.cookie = CookieString

Building or editing the cookie string is the tricky part