/as3/docs/cookies.js

http://wiiflash.googlecode.com/ · JavaScript · 73 lines · 39 code · 3 blank · 31 comment · 7 complexity · ce415dcac1c18d297c645014b20df0d4 MD5 · raw file

  1. /**
  2. * Read the JavaScript cookies tutorial at:
  3. * http://www.netspade.com/articles/javascript/cookies.xml
  4. */
  5. /**
  6. * Sets a Cookie with the given name and value.
  7. *
  8. * name Name of the cookie
  9. * value Value of the cookie
  10. * [expires] Expiration date of the cookie (default: end of current session)
  11. * [path] Path where the cookie is valid (default: path of calling document)
  12. * [domain] Domain where the cookie is valid
  13. * (default: domain of calling document)
  14. * [secure] Boolean value indicating if the cookie transmission requires a
  15. * secure transmission
  16. */
  17. function setCookie(name, value, expires, path, domain, secure)
  18. {
  19. document.cookie= name + "=" + escape(value) +
  20. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  21. ((path) ? "; path=" + path : "") +
  22. ((domain) ? "; domain=" + domain : "") +
  23. ((secure) ? "; secure" : "");
  24. }
  25. /**
  26. * Gets the value of the specified cookie.
  27. *
  28. * name Name of the desired cookie.
  29. *
  30. * Returns a string containing value of specified cookie,
  31. * or null if cookie does not exist.
  32. */
  33. function getCookie(name)
  34. {
  35. var dc = document.cookie;
  36. var prefix = name + "=";
  37. var begin = dc.indexOf("; " + prefix);
  38. if (begin == -1)
  39. {
  40. begin = dc.indexOf(prefix);
  41. if (begin != 0) return null;
  42. }
  43. else
  44. {
  45. begin += 2;
  46. }
  47. var end = document.cookie.indexOf(";", begin);
  48. if (end == -1)
  49. {
  50. end = dc.length;
  51. }
  52. return unescape(dc.substring(begin + prefix.length, end));
  53. }
  54. /**
  55. * Deletes the specified cookie.
  56. *
  57. * name name of the cookie
  58. * [path] path of the cookie (must be same as path used to create cookie)
  59. * [domain] domain of the cookie (must be same as domain used to create cookie)
  60. */
  61. function deleteCookie(name, path, domain)
  62. {
  63. if (getCookie(name))
  64. {
  65. document.cookie = name + "=" +
  66. ((path) ? "; path=" + path : "") +
  67. ((domain) ? "; domain=" + domain : "") +
  68. "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  69. }
  70. }