A unf. social network done poorly.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.7KB

  1. // Cookies API
  2. var cookies = {
  3. // === {{{AjaxIM.}}}**{{{cookies.set(name, value, days)}}}** ===
  4. //
  5. // Sets a cookie, stringifying the JSON value upon storing it.
  6. //
  7. // ==== Parameters ====
  8. // * {{{name}}} is the cookie name.\\
  9. // * {{{value}}} is the cookie data that you would like to store.\\
  10. // * {{{days}}} is the number of days that the cookie will be stored for.
  11. set: function(name, value, days) {
  12. if (days) {
  13. var date = new Date();
  14. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  15. var expires = "; expires=" + date.toGMTString();
  16. } else var expires = "";
  17. document.cookie = name + "=" + JSON.stringify(value) + expires + "; path=/";
  18. },
  19. // === {{{AjaxIM.}}}**{{{cookies.get(name)}}}** ===
  20. //
  21. // Gets a cookie, decoding the JSON value before returning the data.
  22. //
  23. // ==== Parameters ====
  24. // * {{{name}}} is the cookie name that you would like to retrieve.
  25. get: function(name) {
  26. var nameEQ = name + "=";
  27. var ca = document.cookie.split(';');
  28. for(var i = 0; i < ca.length; i++) {
  29. var c = ca[i];
  30. while (c.charAt(0) == ' ') c = c.substring(1, c.length);
  31. if (c.indexOf(nameEQ) == 0) {
  32. var cval = decodeURIComponent(c.substring(nameEQ.length, c.length));
  33. try { return JSON.parse(cval); } catch (e) { return cval; }
  34. }
  35. }
  36. return null;
  37. },
  38. // === {{{AjaxIM.}}}**{{{cookies.erase(name)}}}** ===
  39. //
  40. // Deletes a cookie.
  41. //
  42. // {{{name}}} is the existing cookie that you would like to delete.
  43. erase: function(name) {
  44. this.set(name, '', -1);
  45. }
  46. };