Posted By

wizard04 on 07/30/08


Tagged

javascript cookie


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

ki4ngel
nickdoherty
IvanKD


Cookies


 / Published in: JavaScript
 

Set, get, and erase cookies.

  1. var Cookies = function()
  2. {
  3. //***** private properties and methods *****//
  4.  
  5. //expires argument can be either a Date object or the number of seconds until it should expire
  6. // e.g.: 60: 1 minute from now
  7. // (false): end of session
  8. // -1: remove cookie
  9. function setCookie(name, value, expires, path, domain, secure)
  10. {
  11. if(!name || !name.toString()) return;
  12. value = value.toString() ? escape(value.toString()) : "";
  13.  
  14. var expiration = "";
  15. if(expires)
  16. {
  17. //alert(typeof expires);
  18. if(expires.getTime) expiration = expires.toGMTString(); //expires on given Date
  19. else if(!isNaN(expires)) //expires in given number of seconds
  20. {
  21. var date = new Date();
  22. date.setTime(date.getTime()+(expires*1000));
  23. expiration = date.toGMTString();
  24. }
  25. //else expires at end of session
  26. }
  27. //else expires at end of session
  28.  
  29. path = path || "/";
  30. domain = domain ? "; domain="+domain : "";
  31. secure = secure ? "; secure" : "";
  32. document.cookie = escape(name)+"="+value+"; expires="+expiration+"; path="+path+domain+secure;
  33. }
  34.  
  35. //sets a cookie with an expiration date a number of years into the future
  36. function setPersistentCookie(name, value, years, path, domain, secure)
  37. {
  38. years = years || 50;
  39. setCookie(name, value, years*365*24*60*60, path, domain, secure);
  40. }
  41.  
  42. function getCookie(name)
  43. {
  44. if(!name) return null;
  45. var nameEQ = escape(name) + "=";
  46. var ca = document.cookie.split(";");
  47. var c;
  48. for(var i=0; i<ca.length; i++)
  49. {
  50. c = ca[i];
  51. c = c.replace(/^\s*/, "");
  52. if(c.indexOf(nameEQ) == 0) return unescape(c.slice(nameEQ.length));
  53. }
  54. return null;
  55. }
  56.  
  57. //Returns an array of cookie values; useful when there is more than one cookie with the same name.
  58. //Note that which cookies are returned and in what order depends on the browser, so it's impossible to determine which
  59. // instance of a name corresponds to which domain.
  60. //If a name is provided, it returns an array of values.
  61. //If a name isn't provided, it returns a 2D array of all name/value pairs.
  62. function getCookieArray(name)
  63. {
  64. name = name ? name.toString() : "";
  65. if(!name)
  66. {
  67. var ca = document.cookie.split(";");
  68. var r = [];
  69. for(var i=0; i<ca.length; i++)
  70. {
  71. ca[i] = ca[i].replace(/\s+/g,"");
  72. r[i] = [];
  73. r[i][0] = ca[i].replace(/^(.*?)=.*/i, "$1");
  74. r[i][1] = ca[i].replace(/^.*?=(.*)/i, "$1");
  75. }
  76. return r;
  77. }
  78. else
  79. {
  80. var nameEQ = escape(name) + "=";
  81. var ca = document.cookie.split(";");
  82. var c;
  83. for(var i=0; i<ca.length; i++)
  84. {
  85. ca[i] = ca[i].replace(/\s+/g,"");
  86. if(ca[i].indexOf(nameEQ) != 0) ca.splice(i--,1);
  87. else ca[i] = unescape(ca[i].slice(nameEQ.length))
  88. }
  89. return ca;
  90. }
  91. }
  92.  
  93. function eraseCookie(name, path, domain)
  94. {
  95. setCookie(name, "", -1, path, domain);
  96. }
  97.  
  98. function testSupport()
  99. {
  100. setCookie("test_cookie_support", "test");
  101. var check = getCookie("test_cookie_support");
  102. eraseCookie("test_cookie_support");
  103. return !!check;
  104. }
  105.  
  106. //***** public properties and methods *****//
  107.  
  108. return {
  109. set: setCookie,
  110. setPersistent: setPersistentCookie,
  111. get: getCookie,
  112. getArray: getCookieArray,
  113. erase: eraseCookie,
  114. supported: testSupport()
  115. };
  116. }(); //initialize Cookies

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wizard04 on September 24, 2008

Modified to avoid the unneeded closure.

Posted By: wizard04 on September 24, 2008

Nevermind--scratch that.

Posted By: wizard04 on June 19, 2009

Updated and added Cookies.setPersistent and Cookies.array functions.

Posted By: wizard04 on April 16, 2014

The latest: https://gist.github.com/wizard04wsu/8863347

You need to login to post a comment.