1 define(['jls/lang/Class'], function (Class) { 2 3 var Cookie; 4 /** 5 * @class This class is a collection of Cookie helper functions. 6 * @name jls.util.Cookie 7 */ 8 Cookie = Class.create(); 9 10 Object.extend(Cookie, /** @lends jls.util.Cookie */ 11 { 12 /** 13 * Returns a cookie depending on its name or null if there is no cookie for this name. 14 * 15 * @param {String} name The name of the cookie to get. 16 * @returns {String} The value or null if there is no cookie for this name. 17 */ 18 get : function(name) { 19 var cookies = document.cookie; 20 var prefix = name + '='; 21 var indexOfPrefix = cookies.indexOf('; ' + prefix); 22 if (indexOfPrefix > 0) { 23 indexOfPrefix += 2; 24 } else { 25 indexOfPrefix = cookies.indexOf(prefix); 26 if (indexOfPrefix != 0) { 27 return null; 28 } 29 } 30 var end = document.cookie.indexOf(';', indexOfPrefix); 31 if (end == -1) { 32 end = cookies.length; 33 } 34 return unescape(cookies.substring(indexOfPrefix + prefix.length, end)); 35 }, 36 /** 37 * Sets a cookie. 38 * The expiration date tells the browser when to delete the cookie. 39 * The domain and path tell the browser that the cookie has to be sent back to the server when requesting URLs of a given domain and path. 40 * If not specified, they default to the domain and path of the object that was requested. 41 * As a result, the domain and path strings may tell the browser to send the cookie when it normally would not. 42 * For security reasons, the cookie is accepted only if the server is a member of the domain specified by the domain string. 43 * A secure cookie is only used when a browser is visiting a server via HTTPS. 44 * 45 * @param {String} name The name of the cookie to set. 46 * @param {String} value The value. 47 * @param {Object} [opts] The options. 48 * @param {Date} [opts.expires] The expiration date. 49 * @param {String} [opts.domain] The domain. 50 * @param {String} [opts.path] The path. 51 * @param {String} [opts.secure] The secure. 52 */ 53 set : function(name, value, opts) { 54 var cookie = name + '=' + escape(value); 55 if (opts) { 56 if (('expires' in opts) && (opts.expires != null)) { 57 cookie += '; expires=' + opts.expires.toGMTString(); 58 } 59 if (('domain' in opts) && (opts.domain != null)) { 60 cookie += '; domain=' + opts.domain.toString(); 61 } 62 if (('path' in opts) && (opts.path != null)) { 63 cookie += '; path=' + opts.path.toString(); 64 } 65 if (('secure' in opts) && opts.secure) { 66 cookie += '; secure'; 67 } 68 } 69 document.cookie = cookie; 70 }, 71 /** 72 * Clears a cookie depending on its name. 73 * 74 * @param {String} name The name of the cookie to get. 75 */ 76 clear : function(name) { 77 document.cookie = name + '=; expires=' + new Date(0).toGMTString(); 78 }, 79 /** 80 * Returns all cookies. 81 * 82 * @returns {Object} The cookies. 83 */ 84 all : function() { 85 var cookies = document.cookie; 86 cookies = cookies.split('; '); 87 var all = {}; 88 for (var i = 0; i < cookies.length; i++) { 89 var kv = cookies[i].split('='); 90 all[kv[0]] = kv[1]; 91 } 92 return all; 93 } 94 }); 95 96 return Cookie; 97 }); 98