cookie.js 3.2 KB

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