Creating Cookies
A cookie is a small piece of data that is stored locally by your browser. It contains some data:
- Name - containing the actual data, i.e., the name of the cookie.
- Date - expiry date after which the cookie will no longer be valid
- Domain and Path - the domain and path of the server, where the cookie should be sent to.
As you know that, cookies in JavaScript are accessed using the cookie property of the document object. In other words, a cookie can be created like this:
| Syntax |
| document.cookie = "name=value; expires=date; path=path; domain= domain; secure"; |
To set a cookie, the property document.cookie is set to a string, containing all the properties of the cookie, that you want to create. The syntax of set-cookie is used to transfer significant information between client and server. While assigning a new cookie to the client, in an HTTP response, the following syntax is followed :
| Syntax |
| Set-Cookie : NAME = value [;EXPIRES = date] [;PATH = path] [;DOMAIN = domain] [;SECURE] |
The NAME = value is a compulsory field that must be appended in the Set-Cookie field whereas all the other entries are optional. The server can send more than one Set-Cookie fields.
| Name |
Description |
| NAME = value |
It specifies the name of the cookie, by which you can reference it later. It contains the actual data that the cookie is going to store. The use of semi-colon, comma, or whitespace is illegal in the NAME=value field. |
| EXPIRES = date |
Every cookie persists only for a specific duration. This field specifies the expiry date of the cookie, after which it is scraped, i.e., after this date the cookie will no longer be stored by the client or sent to the server. The syntax for specifying expiry date is :
Weekday, DD-MON-YY HH : MM : SS GMT, Weekday is the day of the week, DD is the day of the month, Mon is the month, YY is the year, HH is hours, MM is minutes and SS is seconds. The GMT is the Greenwich Mean Time. By default, the value of 'Expires' is set to the end of current Navigator session. |
| PATH = path |
The path attribute specifies the portion of the URL for which the cookie is valid, i.e., it specifies the directory where the cookie is active. Usually the path is set to '/' which means the cookie is valid during the entire program. |
| DOMAIN= domain |
This field specifies the domain portion of the URLs for which the cookie is valid. For example, if the cookie is served by www.expert.com, then it also relates to expert.com and www.expert.com/solutions.htm. |
| SECURE |
It specifies that the cookie should be transmitted over a secure link only, i.e., if this field is specified, a cookie is sent to HTTP servers using SSL (Secure Sockets Layer) protocol known as HTTPS servers. If this attribute is not specified, the cookie will be sent over any channel, involving an unsecured one also. |
Retrieving the Cookies
The document.cookie property is again used to retrieve all the previously set cookies applicable for the current document.
| Syntax |
| var c = document.cookie; |
This property will return a string consists of name-value pairs, for all the cookies applicable for the current document. To read the value of a cookie, pass the individual cookie name as a parameter in Get Cookie field.
| Syntax |
| function getCookie (Cookie Name) |
Cookie Name is the name of the cookie as you stored it earlier. The function getCookie( ) returns a string containing the value of the particular cookie or null if the cookie does not exist.
Deleting a Cookie
The function delCookie( ) is used to remove or delete a cookie. It will delete a specified cookie from the browser by setting its expiration date in the past or set day to 0, in Set Cookie. To use this function simply pass the cookie name to the delCookie( ).