4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 var each = tinymce.each;
15 * This class contains simple cookie manangement functions.
17 * @class tinymce.util.Cookie
20 * // Gets a cookie from the browser
21 * console.debug(tinymce.util.Cookie.get('mycookie'));
23 * // Gets a hash table cookie from the browser and takes out the x parameter from it
24 * console.debug(tinymce.util.Cookie.getHash('mycookie').x);
26 * // Sets a hash table cookie to the browser
27 * tinymce.util.Cookie.setHash({x : '1', y : '2'});
29 tinymce.create('static tinymce.util.Cookie', {
31 * Parses the specified query string into an name/value object.
34 * @param {String} n String to parse into a n Hashtable object.
35 * @return {Object} Name/Value object with items parsed from querystring.
37 getHash : function(n) {
38 var v = this.get(n), h;
41 each(v.split('&'), function(v) {
44 h[unescape(v[0])] = unescape(v[1]);
52 * Sets a hashtable name/value object to a cookie.
55 * @param {String} n Name of the cookie.
56 * @param {Object} v Hashtable object to set as cookie.
57 * @param {Date} e Optional date object for the expiration of the cookie.
58 * @param {String} p Optional path to restrict the cookie to.
59 * @param {String} d Optional domain to restrict the cookie to.
60 * @param {String} s Is the cookie secure or not.
62 setHash : function(n, v, e, p, d, s) {
65 each(v, function(v, k) {
66 o += (!o ? '' : '&') + escape(k) + '=' + escape(v);
69 this.set(n, o, e, p, d, s);
73 * Gets the raw data of a cookie by name.
76 * @param {String} n Name of cookie to retrive.
77 * @return {String} Cookie data string.
80 var c = document.cookie, e, p = n + "=", b;
86 b = c.indexOf("; " + p);
96 e = c.indexOf(";", b);
101 return unescape(c.substring(b + p.length, e));
105 * Sets a raw cookie string.
108 * @param {String} n Name of the cookie.
109 * @param {String} v Raw cookie data.
110 * @param {Date} e Optional date object for the expiration of the cookie.
111 * @param {String} p Optional path to restrict the cookie to.
112 * @param {String} d Optional domain to restrict the cookie to.
113 * @param {String} s Is the cookie secure or not.
115 set : function(n, v, e, p, d, s) {
116 document.cookie = n + "=" + escape(v) +
117 ((e) ? "; expires=" + e.toGMTString() : "") +
118 ((p) ? "; path=" + escape(p) : "") +
119 ((d) ? "; domain=" + d : "") +
120 ((s) ? "; secure" : "");
124 * Removes/deletes a cookie by name.
127 * @param {String} n Cookie name to remove/delete.
128 * @param {Strong} p Optional path to remove the cookie from.
130 remove : function(n, p) {
133 d.setTime(d.getTime() - 1000);
135 this.set(n, '', d, p, d);