source: Dev/branches/rest-dojo-ui/client/dojo/cookie.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 2.9 KB
RevLine 
[256]1define(["./_base/kernel", "./regexp"], function(dojo, regexp) {
2        // module:
3        //              dojo/cookie
4        // summary:
5        //              TODOC
6
7
8/*=====
9dojo.__cookieProps = function(){
10        //      expires: Date|String|Number?
11        //              If a number, the number of days from today at which the cookie
12        //              will expire. If a date, the date past which the cookie will expire.
13        //              If expires is in the past, the cookie will be deleted.
14        //              If expires is omitted or is 0, the cookie will expire when the browser closes.
15        //      path: String?
16        //              The path to use for the cookie.
17        //      domain: String?
18        //              The domain to use for the cookie.
19        //      secure: Boolean?
20        //              Whether to only send the cookie on secure connections
21        this.expires = expires;
22        this.path = path;
23        this.domain = domain;
24        this.secure = secure;
25}
26=====*/
27
28
29dojo.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, ret;
54        if(arguments.length == 1){
55                var matches = c.match(new RegExp("(?:^|; )" + regexp.escapeString(name) + "=([^;]*)"));
56                ret = matches ? decodeURIComponent(matches[1]) : 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
68                value = encodeURIComponent(value);
69                var updatedCookie = name + "=" + value, propName;
70                for(propName in props){
71                        updatedCookie += "; " + propName;
72                        var propValue = props[propName];
73                        if(propValue !== true){ updatedCookie += "=" + propValue; }
74                }
75                document.cookie = updatedCookie;
76        }
77        return ret; // String|undefined
78};
79
80dojo.cookie.isSupported = function(){
81        //      summary:
82        //              Use to determine if the current browser supports cookies or not.
83        //
84        //              Returns true if user allows cookies.
85        //              Returns false if user doesn't allow cookies.
86
87        if(!("cookieEnabled" in navigator)){
88                this("__djCookieTest__", "CookiesAllowed");
89                navigator.cookieEnabled = this("__djCookieTest__") == "CookiesAllowed";
90                if(navigator.cookieEnabled){
91                        this("__djCookieTest__", "", {expires: -1});
92                }
93        }
94        return navigator.cookieEnabled;
95};
96
97return dojo.cookie;
98});
Note: See TracBrowser for help on using the repository browser.