Connecting Tech Pros Worldwide Forums | Help | Site Map

Set Get Delete Cookies

Inbaraj's Avatar
Member
 
Join Date: Apr 2007
Location: Puducherry <==> Coimbatore
Posts: 81
#1: Apr 16 '07
Hi...

I want to Set Get Delete Cookies using javascript i saw in net but i cant understand...

Plz help me with sample example.....

with reg
inba

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Apr 16 '07

re: Set Get Delete Cookies


What part do you not understand? Whatever page you saw must have contained some examples.
Inbaraj's Avatar
Member
 
Join Date: Apr 2007
Location: Puducherry <==> Coimbatore
Posts: 81
#3: Apr 17 '07

re: Set Get Delete Cookies


Quote:

Originally Posted by acoder

What part do you not understand? Whatever page you saw must have contained some examples.

I have Set the Cookies using
document.cookies=user.value+" "+pass.value;

I print the Cookies using

alert(document.cookies);

I dont know how to delete the cookies...

help me..

Reg
Inba
Inbaraj's Avatar
Member
 
Join Date: Apr 2007
Location: Puducherry <==> Coimbatore
Posts: 81
#4: Apr 17 '07

re: Set Get Delete Cookies


Hi,,,

I have Create cookies using... javascript...

document.cookies=username.value+" "+pass.value;

To Display the cookies i used the codeing...

alert(document.cookies);

To delete the cookies i dont know... How to do this...

Help me out...

Is it right method to create a cookies.... If i am wrong plz correct me....

Thanks in advance..

With reg
Inbaraj....
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Apr 17 '07

re: Set Get Delete Cookies


Just set the expiry date in the past and the cookies will be deleted.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Apr 17 '07

re: Set Get Delete Cookies


I've merged the threads. They were on the same topic.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Apr 17 '07

re: Set Get Delete Cookies


Quote:

Originally Posted by Inbaraj

Hi,,,

I have Create cookies using... javascript...

document.cookies=username.value+" "+pass.value;
...
Is it right method to create a cookies.... If i am wrong plz correct me....

Thanks in advance..

With reg
Inbaraj....

To create cookies, you need to set an expiry date too, e.g. 1 day, 1 week.
See these two links:
http://www.quirksmode.org/js/cookies.html
http://www.w3schools.com/js/js_cookies.asp
Newbie
 
Join Date: Jul 2007
Posts: 2
#8: Nov 3 '07

re: Set Get Delete Cookies


Here is the Cookies with example

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
Expand|Select|Wrap|Line Numbers
  1. function getCookie(c_name)
  2. {
  3. if (document.cookie.length>0)
  4.   {
  5.   c_start=document.cookie.indexOf(c_name + "=")
  6.   if (c_start!=-1)
  7.     { 
  8.     c_start=c_start + c_name.length+1 
  9.     c_end=document.cookie.indexOf(";",c_start)
  10.     if (c_end==-1) c_end=document.cookie.length
  11.     return unescape(document.cookie.substring(c_start,c_end))
  12.     } 
  13.   }
  14. return ""
  15. }
  16.  
  17. function setCookie(c_name,value,expiredays)
  18. {
  19. var exdate=new Date()
  20. exdate.setDate(exdate.getDate()+expiredays)
  21. document.cookie=c_name+ "=" +escape(value)+
  22. ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
  23. }
  24.  
  25. function checkCookie()
  26. {
  27. username=getCookie('username')
  28. if (username!=null && username!="")
  29.   {alert('Welcome again '+username+'!')}
  30. else 
  31.   {
  32.   username=prompt('Please enter your name:',"")
  33.   if (username!=null && username!="")
  34.     {
  35.     setCookie('username',username,365)
  36.     }
  37.   }
  38. }
Expand|Select|Wrap|Line Numbers
  1. </script>
  2. </head>
  3.  
  4. <body onLoad="checkCookie()">
  5. </body>
  6. </html>
Newbie
 
Join Date: Jul 2007
Posts: 2
#9: Nov 3 '07

re: Set Get Delete Cookies


Cookies full function

Expand|Select|Wrap|Line Numbers
  1. function createCookie(name,value,days) {
  2.     if (days) {
  3.         var date = new Date();
  4.         date.setTime(date.getTime()+(days*24*60*60*1000));
  5.         var expires = "; expires="+date.toGMTString();
  6.     }
  7.     else var expires = "";
  8.     document.cookie = name+"="+value+expires+"; path=/";
  9. }
  10.  
  11. function readCookie(name) {
  12.     var nameEQ = name + "=";
  13.     var ca = document.cookie.split(';');
  14.     for(var i=0;i < ca.length;i++) {
  15.         var c = ca[i];
  16.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  17.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  18.     }
  19.     return null;
  20. }
  21.  
  22. function eraseCookie(name) {
  23.     createCookie(name,"",-1);
  24. }
Reply


Similar JavaScript / Ajax / DHTML bytes