What are cookies?
A cookie is a file containing name value pair and is stored in the browser. It can be of the size 4kb or less.
What purpose do they serve?
The cookie is stored in the browser. When the user visits the domain for which the cookie(s) is/are intended the page can be customised for the viewer and then sent to the browser or values can be set by the browser and then displayed to the user.
So, one can infer that the cookies can be used to store little bits of information in the browser and can be used when needed before it expires.
Parts of a cookie
A cookie has three parts.
· The name/key-value pair: This pair is made of two parts, name/key and value. The name/key helps identify the cookie. The value contains the actual information stored against the name/key.
· Expiry date: This is a date value which specifies when the cookie would expire. If the date is not specified then the cookies will be deleted when the browser gets closed. If not then the cookie continues to live until its expiry date. The date format to be followed is UTC/GMT.
· The domain and path: This specifies the domain and the path which denote that when the browser requests pages from this path and domain the cookies are to be sent to it. The domain and path have to be the ones in which the cookies had been created. One cannot set the cookies for another domain. The path value defaults to ‘/’ which means that all the paths for the domain can access the cookies. If the domain is unspecified then the cookies are assumed to have been set for the particular page which created it.
Here are some examples for cookies.
VisitorName=XYZ; expires= Wed, 24 Sep 2014 20:47:11 UTC; path= ‘/market-views/supermarket.com’
In this cookie the VistorName is the key with which the cookie can be identified. Well, the expiry date is evident here. The domain and the path says that pages that are requested from the path market-views folder in the supermarket.com are to be sent this cookie.
LastVisit= Fri, 12 Sep 2011 03:27:15 UTC; expires= Mon,04 Mar 2017 04:05:20 UTC; path=’www.supermarket.com’
This cookie is sent to the page that has the domain name www.supermarket.com. If we change the path attribute to ‘/supermarket.com’ then all the pages under the domain along with sub domain supermarket.com are sent the cookies.
document.cookie = ‘LastVisit= Fri, 12 Sep 2011 03:27:15 UTC; expires= Mon,04 Mar 2017 04:05:20 UTC; path=’www.supermarket.com’
Now this would create and set the cookie from the page. Now how do we create another cookie? Well, as below.
document.cookie = VisitorName=XYZ; expires= Wed, 24 Sep 2014 20:47:11 UTC; path= ‘/market-views/supermarket.com’
Here the document.cookie is not rewritten with the second statement. Another cookie is created from this. The cookie will be rewritten only if the name/key is repeated.
The method getCookie(name/key) can help retrieve the value for a particular cookie.
greetingDiv.innerHTML = “Hello” + getCookie(“VisitorName”) + “!”;
Deleting a cookie
This is simple. All one has to do is set the value to empty and set the expiration date to -1. Then the cookie will be deleted when the browser exits.