Easy and informative points on Javascript cookies

Some points to be noted about cookies. Trying to clear the doubts of lots of javascript developers who are wary of using cookies in their application. Suggestions are welcome. Tried to cover all the points pertaining to cookies.

Below are few points for all of us to get hands on cookies. These points can clear many doubts on using javascript cookies.

1. Declaring/Initialising a cookie :
document.cookie = "some text/variable here";

2. There are upto 20 cookies stored per domain.

3. The Browser should have cookies enabled. In order for cookies to work.


ESCAPE() method

This is used to convert the special symbols( ; ' / ? { } can create havoc) into hexadecimal values so that browser can read it properly.
Use it whenever you write a cookie

document.cookie = Escape("sometext/variables here")


UNESCAPE() method

This is used to bring back the encoded hexadecimal values to the original values after the browser reads the data.
Use it whenever u read a cookie

var cookie_text = Unescape(document.cookie)

6.An example:

var name = document.form1.textbox1.value
var age = document.form1.textbox2.value
var gender = document.form1.textbox3.value

var cookiestuff= "HI" + name + " " + age + " years" + " sex=" + gender + "." ;

document.cookie = Escape(cookiestuff);

var cookie_text = Unescape(document.cookie)


Cookie Exipiration

When a Browser Session Ends, cookie automatically gets expired. So, we use the cookie expiration date to define the end date of cookies irrespective of the User's Browser session.
"name = [cookie values to be read]; expires=[the cookie expiry date set by the user]"
eg : "name=escape('some text/variable'); expires=[expiry_date]"

var expiry_date = new Date("January 1, 3000)"
expiry_date = expiry_date.toGMTstring();

8.The .toGMTstring converts the date to a value the cookie reads.

9.In order to


just set the expiry date to the past date.

var kill_time = new Date("January 1, 1970");

var kill_string = "name=123 ;expires=" + kill_time.toGMTString();
document.cookie = kill_string;

10. A Cookie Expiration example:

var cookie_value = escape("hi")
var now = new Date()
now.setTime(now.getTime() + 1000*60*60*24*365)
document.cookie= "name=" + cookie_value +"; expires=" + now;

explanation for the eg : the setTime() method gives us an arbitrary value(in milliseconds) from January1, 1970
the getTime() method gives us the current time(in milliseconds) and the settlement is as follows
1000 = milliseconds.
60 = The no. of seconds in a minute.
60 = The no. of minutes in an hour.
24 = The no. of Hours in a day.
365= The no. of days in an year.


Domain Cookies

A cookie can be read by domain which sets them
eg: If the domain .org sets a cookie in its domain, then these cookies wont b read by any other domains(as in .com/.net, etc..)
domain=[.org.in].... This means the cookie can be read only in domains. org.in... n not in any other domain
"name=" + document.cookie(cookie_text) + ";expires=" + now + ";domain=.org.in/path=/special;secure"
Here we can extend the cookies to be accessed to a specific path and if we hav a secure connection(say HTTPS) then we give the secure keyword as shown


Detecting a Cookie

A cookie can be detected simply by using an If condition as follows:

alert('cookie present');
alert('cookie absent')


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: