Sunday, April 4, 2010

How to create cookies in JavaScript


How to create cookies in JavaScript ?


Following example shows how to Create a Cookie with JavaScript.



A Cookie is a piece of data that used to store some information in client machine. Coolies can be used to store user preferences and other related information while he browsing a web site. This data stored on the visitor's computer as a text file. If you visit a web site for the first time, some cookies are created on your machine. If you did not clear browser's cookies, these information are use to load your login data or other preferences when your next visit to the site.

Syntax: document.cookie = name=value; [expires=date];[path=path];[domain=domainname];[secure];
example: document.dookie="user_id=u0012;expires=Tuesday, 29 March 2011 9:51:43 PM;domain=kottawadumi.blogspot.com;path="/"; secure; ";

Cookie attributes must be terminated with a semicolon.

Attributes of a Cookie

  • Cookie Name - Name of the cookie and the value stored in it.
  • Domain - Domain of the server that created the cookie
  • Expiration Date-Expiration date of the cookie. A cookie normally expires when the current browser session ends. But you can specify an expiration date.
  • Path - The path is used to specify where the cookie is valid for a particular server.
  • Secure - If a cookie is secure, it must be sent over a HTTPS server.

All of above attributes are not compulsory. It it is enough to user name and expiration date when creating a cookie. Cookies are domain-specific. It means, if one domain creates a cookie, another domain cannot access it. When assigning name attribute's value to a cookie, you cannot use whitespace, semicolons, or commas. The escape() function will encode the string object by converting all non-alphanumeric characters to their hexadecimal equivalent. You can use unescape() function when get (decode) the string back into its original value.

Demo- Create cookies using JavaScript.





Cookie Name
Cookie Value
 

 

Bookmark and Share




Try It!







1. Example code for create a cookie.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function setCookie(){

var cookieName="UserName";

var cookieValue="Duminda";

var expiryDate = new Date();

var oneYearFromNow = expiryDate.getTime() + (365 * 24 * 60 * 60 * 1000);

expiryDate.setTime(oneYearFromNow);

document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expiryDate.toString()+";";

return true;

}



-->

</script>

HTML Code


<html>

<head><title>How to automatically check multiple check boxes using JavaScript </title>

</head><body onload="javascript:setCookie()"; ><form id="formMultipleCheckBox" name="formMultipleCheckBox"> <table border="0">

</form>

</body>

</html>

1. Example code for check whether the cookie is exists or not.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function ifCookieExists(cookieName){

var cookieData=document.cookie;

var cookieLength=cookieData.length;

var x=0;

var endOfCookie;

var cookieExists=false;

while(x<cookieLength){

if(cookieData.substr(x,(x+cookieName.length))==cookieName){

cookieExists= true;

break;

}

x++;

}

return cookieExists;

}

-->

</script>

1. Example code for read cookie data.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function readCookie(cookieName){

if(ifCookieExists(cookieName)){

var cookieData = document.cookie;

var cookieLength = cookieData.length;

var x = 0;

var endOfCookie;

while (x < cookieLength) {

var y = (x + cookieName.length);

if (cookieData.substring(x,y) == cookieName) {

endOfCookie = cookieData.indexOf(";",y);

if (endOfCookie == -1) {

endOfCookie = cookieData.length;

}

var cData=unescape(cookieData.substring(y+1,endOfCookie));

alert(cData);

}

x++;

}

return "";

}else{

alert("Cookie does not exists");

}

}

-->

</script>

1. Example code for remove a cookie.

JavaScript Code

<script type="text/javascript" language="javascript">

<!--

function removeCookie(cookieName){

if(ifCookieExists(cookieName)){

var expiryDate = new Date();

document.cookie = cookieName+"="+escape("")+";expires="+expiryDate.toString()+";";

alert("The cookie has been removed successfully");

}else{

alert("Cookie does not exists");

}

return true;

}

-->

</script>



©-Copyright By Duminda Chamara   
JavaScript Validation