2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
8
* Utilities for cookie management
9
* @namespace YAHOO.util
12
YAHOO.namespace("util");
21
//-------------------------------------------------------------------------
23
//-------------------------------------------------------------------------
26
* Creates a cookie string that can be assigned into document.cookie.
27
* @param {String} name The name of the cookie.
28
* @param {String} value The value of the cookie.
29
* @param {encodeValue} encodeValue True to encode the value, false to leave as-is.
30
* @param {Object} options (Optional) Options for the cookie.
31
* @return {String} The formatted cookie string.
32
* @method _createCookieString
36
_createCookieString : function (name /*:String*/, value /*:Variant*/, encodeValue /*:Boolean*/, options /*:Object*/) /*:String*/ {
39
var lang = YAHOO.lang;
41
var text /*:String*/ = encodeURIComponent(name) + "=" + (encodeValue ? encodeURIComponent(value) : value);
44
if (lang.isObject(options)){
46
if (options.expires instanceof Date){
47
text += "; expires=" + options.expires.toGMTString();
51
if (lang.isString(options.path) && options.path != ""){
52
text += "; path=" + options.path;
56
if (lang.isString(options.domain) && options.domain != ""){
57
text += "; domain=" + options.domain;
61
if (options.secure === true){
70
* Formats a cookie value for an object containing multiple values.
71
* @param {Object} hash An object of key-value pairs to create a string for.
72
* @return {String} A string suitable for use as a cookie value.
73
* @method _createCookieHash
77
_createCookieHashString : function (hash /*:Object*/) /*:String*/ {
80
var lang = YAHOO.lang;
82
if (!lang.isObject(hash)){
83
throw new TypeError("Cookie._createCookieHashString(): Argument must be an object.");
86
var text /*:Array*/ = new Array();
88
for (var key in hash){
89
if (lang.hasOwnProperty(hash, key) && !lang.isFunction(hash[key]) && !lang.isUndefined(hash[key])){
90
text.push(encodeURIComponent(key) + "=" + encodeURIComponent(String(hash[key])));
94
return text.join("&");
98
* Parses a cookie hash string into an object.
99
* @param {String} text The cookie hash string to parse. The string should already be URL-decoded.
100
* @return {Object} An object containing entries for each cookie value.
101
* @method _parseCookieHash
105
_parseCookieHash : function (text /*:String*/) /*:Object*/ {
107
var hashParts /*:Array*/ = text.split("&"),
108
hashPart /*:Array*/ = null,
109
hash /*:Object*/ = new Object();
111
if (text.length > 0){
112
for (var i=0, len=hashParts.length; i < len; i++){
113
hashPart = hashParts[i].split("=");
114
hash[decodeURIComponent(hashPart[0])] = decodeURIComponent(hashPart[1]);
122
* Parses a cookie string into an object representing all accessible cookies.
123
* @param {String} text The cookie string to parse.
124
* @param {Boolean} decode (Optional) Indicates if the cookie values should be decoded or not. Default is true.
125
* @return {Object} An object containing entries for each accessible cookie.
126
* @method _parseCookieString
130
_parseCookieString : function (text /*:String*/, decode /*:Boolean*/) /*:Object*/ {
132
var cookies /*:Object*/ = new Object();
134
if (YAHOO.lang.isString(text) && text.length > 0) {
136
var decodeValue = (decode === false ? function(s){return s;} : decodeURIComponent);
138
if (/[^=]+=[^=;]?(?:; [^=]+=[^=]?)?/.test(text)){
139
var cookieParts /*:Array*/ = text.split(/;\s/g);
140
var cookieName /*:String*/ = null;
141
var cookieValue /*:String*/ = null;
142
var cookieNameValue /*:Array*/ = null;
144
for (var i=0, len=cookieParts.length; i < len; i++){
146
//check for normally-formatted cookie (name-value)
147
cookieNameValue = cookieParts[i].match(/([^=]+)=/i);
148
if (cookieNameValue instanceof Array){
149
cookieName = decodeURIComponent(cookieNameValue[1]);
150
cookieValue = decodeValue(cookieParts[i].substring(cookieNameValue[1].length+1));
152
//means the cookie does not have an "=", so treat it as a boolean flag
153
cookieName = decodeURIComponent(cookieParts[i]);
154
cookieValue = cookieName;
156
cookies[cookieName] = cookieValue;
164
//-------------------------------------------------------------------------
166
//-------------------------------------------------------------------------
169
* Returns the cookie value for the given name.
170
* @param {String} name The name of the cookie to retrieve.
171
* @param {Function} converter (Optional) A function to run on the value before returning
172
* it. The function is not used if the cookie doesn't exist.
173
* @return {Variant} If no converter is specified, returns a string or null if
174
* the cookie doesn't exist. If the converter is specified, returns the value
175
* returned from the converter or null if the cookie doesn't exist.
179
get : function (name /*:String*/, converter /*:Function*/) /*:Variant*/{
181
var lang = YAHOO.lang;
182
var cookies /*:Object*/ = this._parseCookieString(document.cookie);
184
if (!lang.isString(name) || name === ""){
185
throw new TypeError("Cookie.get(): Cookie name must be a non-empty string.");
188
if (lang.isUndefined(cookies[name])) {
192
if (!lang.isFunction(converter)){
193
return cookies[name];
195
return converter(cookies[name]);
200
* Returns the value of a subcookie.
201
* @param {String} name The name of the cookie to retrieve.
202
* @param {String} subName The name of the subcookie to retrieve.
203
* @param {Function} converter (Optional) A function to run on the value before returning
204
* it. The function is not used if the cookie doesn't exist.
205
* @return {Variant} If the cookie doesn't exist, null is returned. If the subcookie
206
* doesn't exist, null if also returned. If no converter is specified and the
207
* subcookie exists, a string is returned. If a converter is specified and the
208
* subcookie exists, the value returned from the converter is returned.
212
getSub : function (name /*:String*/, subName /*:String*/, converter /*:Function*/) /*:Variant*/ {
214
var lang = YAHOO.lang;
215
var hash /*:Variant*/ = this.getSubs(name);
219
if (!lang.isString(subName) || subName === ""){
220
throw new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string.");
223
if (lang.isUndefined(hash[subName])){
227
if (!lang.isFunction(converter)){
228
return hash[subName];
230
return converter(hash[subName]);
239
* Returns an object containing name-value pairs stored in the cookie with the given name.
240
* @param {String} name The name of the cookie to retrieve.
241
* @return {Object} An object of name-value pairs if the cookie with the given name
242
* exists, null if it does not.
246
getSubs : function (name /*:String*/) /*:Object*/ {
249
if (!YAHOO.lang.isString(name) || name === ""){
250
throw new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string.");
253
var cookies = this._parseCookieString(document.cookie, false);
254
if (YAHOO.lang.isString(cookies[name])){
255
return this._parseCookieHash(cookies[name]);
261
* Removes a cookie from the machine by setting its expiration date to
262
* sometime in the past.
263
* @param {String} name The name of the cookie to remove.
264
* @param {Object} options (Optional) An object containing one or more
265
* cookie options: path (a string), domain (a string),
266
* and secure (true/false). The expires option will be overwritten
268
* @return {String} The created cookie string.
272
remove : function (name /*:String*/, options /*:Object*/) /*:String*/ {
275
if (!YAHOO.lang.isString(name) || name === ""){
276
throw new TypeError("Cookie.remove(): Cookie name must be a non-empty string.");
280
options = options || {};
281
options.expires = new Date(0);
284
return this.set(name, "", options);
288
* Removes a sub cookie with a given name.
289
* @param {String} name The name of the cookie in which the subcookie exists.
290
* @param {String} subName The name of the subcookie to remove.
291
* @param {Object} options (Optional) An object containing one or more
292
* cookie options: path (a string), domain (a string), expires (a Date object),
293
* and secure (true/false). This must be the same settings as the original
295
* @return {String} The created cookie string.
299
removeSub : function(name /*:String*/, subName /*:String*/, options /*:Object*/) /*:String*/ {
302
if (!YAHOO.lang.isString(name) || name === ""){
303
throw new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string.");
306
//check subcookie name
307
if (!YAHOO.lang.isString(subName) || subName === ""){
308
throw new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string.");
311
//get all subcookies for this cookie
312
var subs = this.getSubs(name);
314
//delete the indicated subcookie
315
if (YAHOO.lang.isObject(subs) && YAHOO.lang.hasOwnProperty(subs, subName)){
316
delete subs[subName];
319
return this.setSubs(name, subs, options);
327
* Sets a cookie with a given name and value.
328
* @param {String} name The name of the cookie to set.
329
* @param {Variant} value The value to set for the cookie.
330
* @param {Object} options (Optional) An object containing one or more
331
* cookie options: path (a string), domain (a string), expires (a Date object),
332
* and secure (true/false).
333
* @return {String} The created cookie string.
337
set : function (name /*:String*/, value /*:Variant*/, options /*:Object*/) /*:String*/ {
339
var lang = YAHOO.lang;
341
if (!lang.isString(name)){
342
throw new TypeError("Cookie.set(): Cookie name must be a string.");
345
if (lang.isUndefined(value)){
346
throw new TypeError("Cookie.set(): Value cannot be undefined.");
350
var text /*:String*/ = this._createCookieString(name, value, true, options);
351
document.cookie = text;
356
* Sets a sub cookie with a given name to a particular value.
357
* @param {String} name The name of the cookie to set.
358
* @param {String} subName The name of the subcookie to set.
359
* @param {Variant} value The value to set.
360
* @param {Object} options (Optional) An object containing one or more
361
* cookie options: path (a string), domain (a string), expires (a Date object),
362
* and secure (true/false).
363
* @return {String} The created cookie string.
367
setSub : function (name /*:String*/, subName /*:String*/, value /*:Variant*/, options /*:Object*/) /*:String*/ {
369
var lang = YAHOO.lang;
371
if (!lang.isString(name) || name === ""){
372
throw new TypeError("Cookie.setSub(): Cookie name must be a non-empty string.");
375
if (!lang.isString(subName) || subName === ""){
376
throw new TypeError("Cookie.setSub(): Subcookie name must be a non-empty string.");
379
if (lang.isUndefined(value)){
380
throw new TypeError("Cookie.setSub(): Subcookie value cannot be undefined.");
383
var hash /*:Object*/ = this.getSubs(name);
385
if (!lang.isObject(hash)){
389
hash[subName] = value;
391
return this.setSubs(name, hash, options);
396
* Sets a cookie with a given name to contain a hash of name-value pairs.
397
* @param {String} name The name of the cookie to set.
398
* @param {Object} value An object containing name-value pairs.
399
* @param {Object} options (Optional) An object containing one or more
400
* cookie options: path (a string), domain (a string), expires (a Date object),
401
* and secure (true/false).
402
* @return {String} The created cookie string.
406
setSubs : function (name /*:String*/, value /*:Object*/, options /*:Object*/) /*:String*/ {
408
var lang = YAHOO.lang;
410
if (!lang.isString(name)){
411
throw new TypeError("Cookie.setSubs(): Cookie name must be a string.");
414
if (!lang.isObject(value)){
415
throw new TypeError("Cookie.setSubs(): Cookie value must be an object.");
418
var text /*:String*/ = this._createCookieString(name, this._createCookieHashString(value), false, options);
419
document.cookie = text;
424
YAHOO.register("cookie", YAHOO.util.Cookie, {version: "2.6.0", build: "1321"});