cookie.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. define(["./_base/kernel", "./regexp"], function(dojo, regexp){
  2. // module:
  3. // dojo/cookie
  4. /*=====
  5. var __cookieProps = {
  6. // expires: Date|String|Number?
  7. // If a number, the number of days from today at which the cookie
  8. // will expire. If a date, the date past which the cookie will expire.
  9. // If expires is in the past, the cookie will be deleted.
  10. // If expires is omitted or is 0, the cookie will expire when the browser closes.
  11. // path: String?
  12. // The path to use for the cookie.
  13. // domain: String?
  14. // The domain to use for the cookie.
  15. // secure: Boolean?
  16. // Whether to only send the cookie on secure connections
  17. };
  18. =====*/
  19. dojo.cookie = function(/*String*/name, /*String?*/ value, /*__cookieProps?*/ props){
  20. // summary:
  21. // Get or set a cookie.
  22. // description:
  23. // If one argument is passed, returns the value of the cookie
  24. // For two or more arguments, acts as a setter.
  25. // name:
  26. // Name of the cookie
  27. // value:
  28. // Value for the cookie
  29. // props:
  30. // Properties for the cookie
  31. // example:
  32. // set a cookie with the JSON-serialized contents of an object which
  33. // will expire 5 days from now:
  34. // | require(["dojo/cookie", "dojo/json"], function(cookie, json){
  35. // | cookie("configObj", json.stringify(config, {expires: 5 }));
  36. // | });
  37. //
  38. // example:
  39. // de-serialize a cookie back into a JavaScript object:
  40. // | require(["dojo/cookie", "dojo/json"], function(cookie, json){
  41. // | config = json.parse(cookie("configObj"));
  42. // | });
  43. //
  44. // example:
  45. // delete a cookie:
  46. // | require(["dojo/cookie"], function(cookie){
  47. // | cookie("configObj", null, {expires: -1});
  48. // | });
  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. });