Posted By

MMDeveloper on 09/16/08


Tagged

javascript cookie


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

SpinZ
rwczippy


JavaScript Cookie Management Class


 / Published in: JavaScript
 

URL: http://www.mechanicmatt.com

  1. var JSCookies = {
  2.  
  3. defaults: {
  4. expiryDays: 7
  5. },
  6.  
  7. createCookie: function(name, value, days) {
  8. if (!days) {
  9. days = this.defaults.expiryDays;
  10. } else {}
  11.  
  12. date = new Date();
  13. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  14. expires = "; expires=" + date.toGMTString();
  15.  
  16. document.cookie = name + "=" + value + expires + "; path=/";
  17.  
  18. return {"name": name, "value": value};
  19. },
  20.  
  21. readCookie: function(name) {
  22. nameEQ = name + "=";
  23. ca = $A(document.cookie.split(";"));
  24. for (i = 0; i < ca.length; i++) {
  25. c = ca[i];
  26. while (c.charAt(0) == " ") {
  27. c = c.substring(1, c.length);
  28. }
  29. if (c.indexOf(nameEQ) == 0) {
  30. return c.substring(nameEQ.length, c.length);
  31. } else {}
  32. }
  33. return "";
  34. },
  35.  
  36. eraseCookie: function(name) {
  37. this.createCookie(name, "", -1);
  38. return {"name": name, "value": null};
  39. }
  40. };
  41.  
  42. JSCookies.createCookie("name", "value");
  43.  
  44.  
  45. //next page
  46. alert(JSCookies.readCookie("name"));

Report this snippet  

You need to login to post a comment.