Javascript – Cookies

Javascript – Cookies

When a user requests a page, an HTTP request  is sent to the server. The request includes a header that defines several pieces of  information.  Including the page being requested.

The server returns an HTTP response that also includes a header. The header contains information about the document being returned, including  its MIME type. These headers al l contain one or more fields of information in a basic format.

Cookies information is shared between the client browser and a server using fields in the HTTP headers.

When the user requests a page for the  first time ,  a cookie can be stored in the browser by a set-cookie entry in the header to the response from the server. The set-cookie field includes the information to be stored in the cookie along with several optional pieces of information including an expiry date, path , and server information and If the cookie requires security.

When the user requests a page in the future, if a matching cookie is found among all the stored cookies , the browser sends a cookie field to the server in a request header. The header will contain the information stored in that cookie

Parts of cookies  :-

Name =value    [; expires=date] [;domain=domain]; [; path=path] [; secure]


Name=value    :- sets the cookie named

Example :- username =tap;


Expires=date  :-  Sets the expiration date of the cookie to date. The date string is given in Internet standard GMT format. To format a Date to this specification, you can use the toGMTString() method of Date instances.

Example :- expires=sun,  08 nov 2016       ,   08: 00:00 GMT


Domain=domain   :- sets the domain for the cookie to domain, which  must correspond (with certain flexibility ) to the domain of the server setting the cookie. The cookie will be returned only when making a  request of this domain.

Example :-

Path=path  = String  indicating the subset of paths at the domain for which the cookie will be returned.

Example :-  path=/users/Thomas/

Secure :- indicates that the cookie is only to be returned over a secure (HTTPPS) connection.

Example : – secure


Setting Cookies :- When you assign a string to document.cookie, the browser parses it as a cookie and adds it to its list of cookies. For  Example :- the following sets a persistent cookie named username with value “mark” that expires in 2016 and will be sent whenever a request is made for  a file under the “/home” directory on the current web server.

Document.cookie=”username=mark”; expires=sun, 08-nov-2016 08:00:00GMT ; path=/home”;


The cookie-parsing routines used by the browser assume that nay cookies you set are well formed. The name-value pair must not contain any white space characters.

When the old cookies values replaced by new cookies

Example :-



Alert(“Cookies :”+ document.cookie);


Example of cookies :-

<title> I am working on javascript</title>
var cookies={};
function extractCookies(){
cookies={ };
var cookieArray=document.cookie.split(“;”);
for(var i=0, len=cookieArray.length; i<len; i++){

var cookie=cookieArray[i];



var cookiePieces=cookie.split(“=”);
var name=cookiePieces[0];
var value=cookiePieces[1];


alert(“value :” +value);


Follow Us

Contact Us

Back to Top