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