0cc4e5e61d365f7ee89d2dcd3f5b2e5791c5c04c.svn-base 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. if (!dojo._hasResource["dojo.cookie"]) { // _hasResource checks added by
  2. // build. Do not use _hasResource
  3. // directly in your code.
  4. dojo._hasResource["dojo.cookie"] = true;
  5. dojo.provide("dojo.cookie");
  6. /*
  7. * ===== dojo.__cookieProps = function(kwArgs){ // expires: Date|Number? //
  8. * If a number, seen as the number of days from today. If a date, the //
  9. * date past which the cookie is invalid. If expires is in the past, // the
  10. * cookie will be deleted If expires is left out or is 0, the // cookie will
  11. * expire when the browser closes. // path: String? // The path to use for
  12. * the cookie. // domain: String? // The domain to use for the cookie. //
  13. * secure: Boolean? // Whether to only send the cookie on secure connections }
  14. * =====
  15. */
  16. dojo.cookie = function(/* String */name, /* String? */value, /* dojo.__cookieProps? */
  17. props) {
  18. // summary:
  19. // Get or set a cookie.
  20. // description:
  21. // If you pass in one argument, the the value of the cookie is returned
  22. //
  23. // If you pass in two arguments, the cookie value is set to the second
  24. // argument.
  25. //
  26. // If you pass in three arguments, the cookie value is set to the
  27. // second argument, and the options on the third argument are used for
  28. // extended properties on the cookie
  29. // name:
  30. // The name of the cookie
  31. // value:
  32. // Optional. The value for the cookie.
  33. // props:
  34. // Optional additional properties for the cookie
  35. // example:
  36. // set a cookie with the JSON-serialized contents of an object which
  37. // will expire 5 days from now:
  38. // | dojo.cookie("configObj", dojo.toJson(config), { expires: 5 });
  39. //
  40. // example:
  41. // de-serialize a cookie back into a JavaScript object:
  42. // | var config = dojo.fromJson(dojo.cookie("configObj"));
  43. //
  44. // example:
  45. // delete a cookie:
  46. // | dojo.cookie("configObj", null);
  47. var c = document.cookie;
  48. if (arguments.length == 1) {
  49. var idx = c.lastIndexOf(name + '=');
  50. if (idx == -1) {
  51. return null;
  52. }
  53. var start = idx + name.length + 1;
  54. var end = c.indexOf(';', idx + name.length + 1);
  55. if (end == -1) {
  56. end = c.length;
  57. }
  58. return decodeURIComponent(c.substring(start, end));
  59. } else {
  60. props = props || {};
  61. value = encodeURIComponent(value);
  62. if (typeof(props.expires) == "number") {
  63. var d = allGetServerTime();
  64. d.setTime(d.getTime() + (props.expires * 24 * 60 * 60 * 1000));
  65. props.expires = d;
  66. }
  67. document.cookie = name
  68. + "="
  69. + value
  70. + (props.expires ? "; expires="
  71. + props.expires.toUTCString() : "")
  72. + (props.path ? "; path=" + props.path : "")
  73. + (props.domain ? "; domain=" + props.domain : "")
  74. + (props.secure ? "; secure" : "");
  75. return null;
  76. }
  77. };
  78. }