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.
4. 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")
5.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)
7. 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 KILL COOKIES
just set the expiry date to the past date.
eg:
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.
11. 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
12. Detecting a Cookie
:
A cookie can be detected simply by using an If condition as follows:
If(document.cookie)
{
alert('cookie present');
}
else
{
alert('cookie absent')
}