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