]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/cookie.js
2 Copyright (c) 2004-2010, 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
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;
10 dojo
.provide("dojo.cookie");
12 dojo
.require("dojo.regexp");
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.
22 // The path to use for the cookie.
24 // The domain to use for the cookie.
26 // Whether to only send the cookie on secure connections
27 this.expires = expires;
35 dojo
.cookie = function(/*String*/name
, /*String?*/value
, /*dojo.__cookieProps?*/props
){
37 // Get or set a cookie.
39 // If one argument is passed, returns the value of the cookie
40 // For two or more arguments, acts as a setter.
44 // Value for the cookie
46 // Properties for the cookie
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 });
53 // de-serialize a cookie back into a JavaScript object:
54 // | var config = dojo.fromJson(dojo.cookie("configObj"));
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
65 // FIXME: expires=0 seems to disappear right away, not on close? (FF3) Change docs?
66 var exp
= props
.expires
;
67 if(typeof exp
== "number"){
69 d
.setTime(d
.getTime() + exp
*24*60*60*1000);
70 exp
= props
.expires
= d
;
72 if(exp
&& exp
.toUTCString
){ props
.expires
= exp
.toUTCString(); }
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
; }
81 document
.cookie
= updatedCookie
;
85 dojo
.cookie
.isSupported = function(){
87 // Use to determine if the current browser supports cookies or not.
89 // Returns true if user allows cookies.
90 // Returns false if user doesn't allow cookies.
92 if(!("cookieEnabled" in navigator
)){
93 this("__djCookieTest__", "CookiesAllowed");
94 navigator
.cookieEnabled
= this("__djCookieTest__") == "CookiesAllowed";
95 if(navigator
.cookieEnabled
){
96 this("__djCookieTest__", "", {expires
: -1});
99 return navigator
.cookieEnabled
;