Wednesday 15 January 2014

Javascript : Working with Cookies - Part - 2

Storing Cookies:

We can create a cookie and assign it to a string value to the document.cookie object:
Syntax:
document.cookie = "key1=value1;key2=value2;expires=date";
if we set cookie with given date and time the cookie will expires and
cannot be accessible

Note: Cookie values may not include semicolons, commas, or whitespace. For this reason,
you may need to use the JavaScript escape() function to encode the value before storing
it in the cookie. and unescape() function to read the cookie

Reading Cookies:
Reading a cookie is just as simple as writing one, because the
value of the document.cookie object is the cookie. So you can
use this string whenever you want to access the cookie.

The document.cookie string will keep a list of name=value pairs
separated by semicolons, where name is the name of a cookie and
value is its string value.

You can use strings' split() function to break the string into
key and values as follows:


Deleting a Cookie:
Sometimes you will want to delete a cookie so that subsequent attempts to 
read the cookie return nothing. To do this, you just need to set the 
expiration date to a time in the past.


No comments:

Post a Comment