source: Dev/trunk/src/client/dojo/cookie.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 3.0 KB
RevLine 
[483]1define(["./_base/kernel", "./regexp"], function(dojo, regexp){
2
3// module:
4//              dojo/cookie
5
6/*=====
7var __cookieProps = {
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};
20=====*/
21
22
23dojo.cookie = function(/*String*/name, /*String?*/ value, /*__cookieProps?*/ props){
24        // summary:
25        //              Get or set a cookie.
26        // description:
27        //              If one argument is passed, returns the value of the cookie
28        //              For two or more arguments, acts as a setter.
29        // name:
30        //              Name of the cookie
31        // value:
32        //              Value for the cookie
33        // props:
34        //              Properties for the cookie
35        // example:
36        //              set a cookie with the JSON-serialized contents of an object which
37        //              will expire 5 days from now:
38        //      |       require(["dojo/cookie", "dojo/json"], function(cookie, json){
39        //      |               cookie("configObj", json.stringify(config, {expires: 5 }));
40        //      |       });
41        //
42        // example:
43        //              de-serialize a cookie back into a JavaScript object:
44        //      |       require(["dojo/cookie", "dojo/json"], function(cookie, json){
45        //      |               config = json.parse(cookie("configObj"));
46        //      |       });
47        //
48        // example:
49        //              delete a cookie:
50        //      |       require(["dojo/cookie"], function(cookie){
51        //      |               cookie("configObj", null, {expires: -1});
52        //      |       });
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.