Connecting Tech Pros Worldwide Help | Site Map

Set Get Delete Cookies

 
LinkBack Thread Tools Search this Thread
  #1  
Old April 16th, 2007, 06:08 AM
Inbaraj's Avatar
Member
 
Join Date: Apr 2007
Location: Puducherry <==> Coimbatore
Posts: 81
Default Set Get Delete Cookies

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
Reply
  #2  
Old April 16th, 2007, 10:58 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,232
Default

What part do you not understand? Whatever page you saw must have contained some examples.
Reply
  #3  
Old April 17th, 2007, 04:59 AM
Inbaraj's Avatar
Member
 
Join Date: Apr 2007
Location: Puducherry <==> Coimbatore
Posts: 81
Default

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
Reply
  #4  
Old April 17th, 2007, 05:02 AM
Inbaraj's Avatar
Member
 
Join Date: Apr 2007
Location: Puducherry <==> Coimbatore
Posts: 81
Default Create 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....
Reply
  #5  
Old April 17th, 2007, 11:20 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,232
Default

Just set the expiry date in the past and the cookies will be deleted.
Reply
  #6  
Old April 17th, 2007, 11:21 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,232
Default

I've merged the threads. They were on the same topic.
Reply
  #7  
Old April 17th, 2007, 11:24 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,232
Default

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
Reply
  #8  
Old November 3rd, 2007, 05:51 AM
Newbie
 
Join Date: Jul 2007
Posts: 2
Default

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>

Last edited by acoder; November 3rd, 2007 at 08:33 AM. Reason: Added code tags
Reply
  #9  
Old November 3rd, 2007, 05:52 AM
Newbie
 
Join Date: Jul 2007
Posts: 2
Default

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. }

Last edited by acoder; November 3rd, 2007 at 08:34 AM. Reason: Added code tags
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.