By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Set Get Delete Cookies

Inbaraj
P: 76
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
Apr 16 '07 #1
Share this Question
Share on Google+
8 Replies


acoder
Expert Mod 15k+
P: 16,027
What part do you not understand? Whatever page you saw must have contained some examples.
Apr 16 '07 #2

Inbaraj
P: 76
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
Apr 17 '07 #3

Inbaraj
P: 76
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....
Apr 17 '07 #4

acoder
Expert Mod 15k+
P: 16,027
Just set the expiry date in the past and the cookies will be deleted.
Apr 17 '07 #5

acoder
Expert Mod 15k+
P: 16,027
I've merged the threads. They were on the same topic.
Apr 17 '07 #6

acoder
Expert Mod 15k+
P: 16,027
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
Apr 17 '07 #7

P: 2
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>
Nov 3 '07 #8

P: 2
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. }
Nov 3 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.