cookie.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. define("dojo/cookie", ["./_base/kernel", "./regexp"], function(dojo, regexp) {
  2. // module:
  3. // dojo/cookie
  4. // summary:
  5. // TODOC
  6. /*=====
  7. dojo.__cookieProps = function(){
  8. // expires: Date|String|Number?
  9. // If a number, the number of days from today at which the cookie
  10. // will expire. If a date, the date past which the cookie will expire.
  11. // If expires is in the past, the cookie will be deleted.
  12. // If expires is omitted or is 0, the cookie will expire when the browser closes.
  13. // path: String?
  14. // The path to use for the cookie.
  15. // domain: String?
  16. // The domain to use for the cookie.
  17. // secure: Boolean?
  18. // Whether to only send the cookie on secure connections
  19. this.expires = expires;
  20. this.path = path;
  21. this.domain = domain;
  22. this.secure = secure;
  23. }
  24. =====*/
  25. dojo.cookie = function(/*String*/name, /*String?*/value, /*dojo.__cookieProps?*/props){
  26. // summary:
  27. // Get or set a cookie.
  28. // description:
  29. // If one argument is passed, returns the value of the cookie
  30. // For two or more arguments, acts as a setter.
  31. // name:
  32. // Name of the cookie
  33. // value:
  34. // Value for the cookie
  35. // props:
  36. // Properties for the cookie
  37. // example:
  38. // set a cookie with the JSON-serialized contents of an object which
  39. // will expire 5 days from now:
  40. // | dojo.cookie("configObj", dojo.toJson(config), { expires: 5 });
  41. //
  42. // example:
  43. // de-serialize a cookie back into a JavaScript object:
  44. // | var config = dojo.fromJson(dojo.cookie("configObj"));
  45. //
  46. // example:
  47. // delete a cookie:
  48. // | dojo.cookie("configObj", null, {expires: -1});
  49. var c = document.cookie, ret;
  50. if(arguments.length == 1){
  51. var matches = c.match(new RegExp("(?:^|; )" + regexp.escapeString(name) + "=([^;]*)"));
  52. ret = matches ? decodeURIComponent(matches[1]) : undefined;
  53. }else{
  54. props = props || {};
  55. // FIXME: expires=0 seems to disappear right away, not on close? (FF3) Change docs?
  56. var exp = props.expires;
  57. if(typeof exp == "number"){
  58. var d = new Date();
  59. d.setTime(d.getTime() + exp*24*60*60*1000);
  60. exp = props.expires = d;
  61. }
  62. if(exp && exp.toUTCString){ props.expires = exp.toUTCString(); }
  63. value = encodeURIComponent(value);
  64. var updatedCookie = name + "=" + value, propName;
  65. for(propName in props){
  66. updatedCookie += "; " + propName;
  67. var propValue = props[propName];
  68. if(propValue !== true){ updatedCookie += "=" + propValue; }
  69. }
  70. document.cookie = updatedCookie;
  71. }
  72. return ret; // String|undefined
  73. };
  74. dojo.cookie.isSupported = function(){
  75. // summary:
  76. // Use to determine if the current browser supports cookies or not.
  77. //
  78. // Returns true if user allows cookies.
  79. // Returns false if user doesn't allow cookies.
  80. if(!("cookieEnabled" in navigator)){
  81. this("__djCookieTest__", "CookiesAllowed");
  82. navigator.cookieEnabled = this("__djCookieTest__") == "CookiesAllowed";
  83. if(navigator.cookieEnabled){
  84. this("__djCookieTest__", "", {expires: -1});
  85. }
  86. }
  87. return navigator.cookieEnabled;
  88. };
  89. return dojo.cookie;
  90. });