Changeset 12333
- Timestamp:
- 02/09/08 19:23:35 (11 months ago)
- Location:
- dojo/trunk
- Files:
-
- 2 modified
-
cookie.js (modified) (4 diffs)
-
tests/cookie.html (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
dojo/trunk/cookie.js
r11834 r12333 1 1 dojo.provide("dojo.cookie"); 2 3 dojo.require("dojo.regexp"); 2 4 3 5 /*===== 4 6 dojo.__cookieProps = function(kwArgs){ 5 // expires: Date| Number?6 // If a number, seen as the number of days from today. If a date, the7 // date past which the cookie is invalid. If expires is in the past,8 // the cookie will be deleted If expires is left out or is 0, the9 // cookie will expire when the browser closes.7 // expires: Date|String|Number? 8 // If a number, the number of days from today at which the cookie 9 // will expire. If a date, the date past which the cookie will expire. 10 // If expires is in the past, the cookie will be deleted. 11 // If expires is omitted or is 0, the cookie will expire when the browser closes. 10 12 // path: String? 11 13 // The path to use for the cookie. … … 22 24 // Get or set a cookie. 23 25 // description: 24 // If you pass in one argument, the the value of the cookie is returned 25 // 26 // If you pass in two arguments, the cookie value is set to the second 27 // argument. 28 // 29 // If you pass in three arguments, the cookie value is set to the 30 // second argument, and the options on the third argument are used for 31 // extended properties on the cookie 26 // If one argument is passed, returns the value of the cookie 27 // For two or more arguments, acts as a setter. 32 28 // name: 33 // The name of the cookie29 // Name of the cookie 34 30 // value: 35 // Optional. The value for the cookie.31 // Value for the cookie 36 32 // props: 37 // Optional additional properties for the cookie33 // Properties for the cookie 38 34 // example: 39 35 // set a cookie with the JSON-serialized contents of an object which … … 47 43 // example: 48 44 // delete a cookie: 49 // | dojo.cookie("configObj", null );45 // | dojo.cookie("configObj", null, {expires: -1}); 50 46 var c = document.cookie; 51 47 if(arguments.length == 1){ 52 var idx = c.lastIndexOf(name+'='); 53 if(idx == -1){ return null; } 54 var start = idx+name.length+1; 55 var end = c.indexOf(';', idx+name.length+1); 56 if(end == -1){ end = c.length; } 57 return decodeURIComponent(c.substring(start, end)); 48 var matches = c.match(new RegExp("(?:^|(?:; ))" + dojo.regexp.escapeString(name) + "=([^;]*)")); 49 return matches ? decodeURIComponent(matches[1]) : undefined; // String or undefined 58 50 }else{ 59 51 props = props || {}; 60 value = encodeURIComponent(value); 61 if(typeof(props.expires) == "number"){ 52 // FIXME: expires=0 seems to disappear right away, not on close? (FF3) Change docs? 53 var exp = props.expires; 54 if(typeof exp == "number"){ 62 55 var d = new Date(); 63 d.setTime(d.getTime() +(props.expires*24*60*60*1000));56 d.setTime(d.getTime() + exp*24*60*60*1000); 64 57 props.expires = d; 65 58 } 66 document.cookie = name + "=" + value 67 + (props.expires ? "; expires=" + props.expires.toUTCString() : "") 68 + (props.path ? "; path=" + props.path : "") 69 + (props.domain ? "; domain=" + props.domain : "") 70 + (props.secure ? "; secure" : ""); 71 return null; 59 if(exp && exp.toUTCString){ props.expires = exp.toUTCString(); } 60 61 value = encodeURIComponent(value); 62 var updatedCookie = name + "=" + value; 63 for(propName in props){ 64 updatedCookie += "; " + propName; 65 var propValue = props[propName]; 66 if(propValue !== true){ updatedCookie += "=" + propValue; } 67 } 68 document.cookie = updatedCookie; 72 69 } 73 70 }; … … 94 91 this(name, dojo.toJson(value), props||{}); 95 92 } 96 97 93 }; 98 94 -
dojo/trunk/tests/cookie.html
r11834 r12333 29 29 var start = document.cookie.indexOf(n+"=") + n.length + 1; 30 30 var end = document.cookie.indexOf(";", start); 31 if(end == -1) end = document.cookie.length;31 if(end == -1){ end = document.cookie.length; } 32 32 t.is(v, decodeURIComponent(document.cookie.substring(start, end))); 33 33 } … … 66 66 t.is(null, dojo.cookie("dojo_num")); 67 67 } 68 }, 69 { 70 name: "nameSuffix", 71 runTest: function(t){ 72 // set two cookies with the same suffix 73 dojo.cookie("user", "123", { expires: 10 }); 74 dojo.cookie("xuser", "abc", { expires: 10 }); 75 t.is("123", dojo.cookie("user")); 76 t.is("abc", dojo.cookie("xuser")); 77 78 // remove the cookie by setting it with a negative 79 // numerical expires. value doesn't really matter here 80 dojo.cookie("user", "-deleted-", { expires: -10 }); 81 t.is(null, dojo.cookie("user")); 82 dojo.cookie("xuser", "-deleted-", { expires: -10 }); 83 t.is(null, dojo.cookie("xuser")); 84 } 68 85 } 69 86 ]