Cookies using Javascript
Hi all,
Today , I was working on my project and I was required to work on the cookies in the client side. Infact , I have to read and write cookie from both client side and server side. For server side it was easy to read , write and expire the cookie but I need to work bit hard in javascript for the same.
Now, I will tell you the method I wrote to read, create and expire the cookie.
//Creating Cookie
function CreateCookie(cookieName,value,hrs)
{
if (hrs)
{
var date = new Date();
date.setTime(date.getTime()+(hrs*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
document.cookie = cookieName+"="+value+expires+"; path=/";
}
//Reading Cookie Value
function ReadCookie(cookieName) {
var nameEQ = cookieName + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
//Deleting Cookie
function EraseCookie(cookieName) {
createCookie(cookieName,"",-10);
}
I hope the method I wrote are self explanatory .
But if you still have any issue, please feel free to contact/reply me.
Thanks
Arvind-Ramp
Happy Programming
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment