Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / util / Cookies.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.util.Cookies\r
9  * Utility class for managing and interacting with cookies.\r
10  * @singleton\r
11  */\r
12 Ext.util.Cookies = {\r
13     /**\r
14      * Create a cookie with the specified name and value. Additional settings\r
15      * for the cookie may be optionally specified (for example: expiration,\r
16      * access restriction, SSL).\r
17      * @param {String} name The name of the cookie to set. \r
18      * @param {Mixed} value The value to set for the cookie.\r
19      * @param {Object} expires (Optional) Specify an expiration date the\r
20      * cookie is to persist until.  Note that the specified Date object will\r
21      * be converted to Greenwich Mean Time (GMT). \r
22      * @param {String} path (Optional) Setting a path on the cookie restricts\r
23      * access to pages that match that path. Defaults to all pages (<tt>'/'</tt>). \r
24      * @param {String} domain (Optional) Setting a domain restricts access to\r
25      * pages on a given domain (typically used to allow cookie access across\r
26      * subdomains). For example, "extjs.com" will create a cookie that can be\r
27      * accessed from any subdomain of extjs.com, including www.extjs.com,\r
28      * support.extjs.com, etc.\r
29      * @param {Boolean} secure (Optional) Specify true to indicate that the cookie\r
30      * should only be accessible via SSL on a page using the HTTPS protocol.\r
31      * Defaults to <tt>false</tt>. Note that this will only work if the page\r
32      * calling this code uses the HTTPS protocol, otherwise the cookie will be\r
33      * created with default options.\r
34      */\r
35     set : function(name, value){\r
36         var argv = arguments;\r
37         var argc = arguments.length;\r
38         var expires = (argc > 2) ? argv[2] : null;\r
39         var path = (argc > 3) ? argv[3] : '/';\r
40         var domain = (argc > 4) ? argv[4] : null;\r
41         var secure = (argc > 5) ? argv[5] : false;\r
42         document.cookie = name + "=" + escape(value) + ((expires === null) ? "" : ("; expires=" + expires.toGMTString())) + ((path === null) ? "" : ("; path=" + path)) + ((domain === null) ? "" : ("; domain=" + domain)) + ((secure === true) ? "; secure" : "");\r
43     },\r
44 \r
45     /**\r
46      * Retrieves cookies that are accessible by the current page. If a cookie\r
47      * does not exist, <code>get()</code> returns <tt>null</tt>.  The following\r
48      * example retrieves the cookie called "valid" and stores the String value\r
49      * in the variable <tt>validStatus</tt>.\r
50      * <pre><code>\r
51      * var validStatus = Ext.util.Cookies.get("valid");\r
52      * </code></pre>\r
53      * @param {String} name The name of the cookie to get\r
54      * @return {Mixed} Returns the cookie value for the specified name;\r
55      * null if the cookie name does not exist.\r
56      */\r
57     get : function(name){\r
58         var arg = name + "=";\r
59         var alen = arg.length;\r
60         var clen = document.cookie.length;\r
61         var i = 0;\r
62         var j = 0;\r
63         while(i < clen){\r
64             j = i + alen;\r
65             if(document.cookie.substring(i, j) == arg){\r
66                 return Ext.util.Cookies.getCookieVal(j);\r
67             }\r
68             i = document.cookie.indexOf(" ", i) + 1;\r
69             if(i === 0){\r
70                 break;\r
71             }\r
72         }\r
73         return null;\r
74     },\r
75 \r
76     /**\r
77      * Removes a cookie with the provided name from the browser\r
78      * if found by setting its expiration date to sometime in the past. \r
79      * @param {String} name The name of the cookie to remove\r
80      */\r
81     clear : function(name){\r
82         if(Ext.util.Cookies.get(name)){\r
83             document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";\r
84         }\r
85     },\r
86     /**\r
87      * @private\r
88      */\r
89     getCookieVal : function(offset){\r
90         var endstr = document.cookie.indexOf(";", offset);\r
91         if(endstr == -1){\r
92             endstr = document.cookie.length;\r
93         }\r
94         return unescape(document.cookie.substring(offset, endstr));\r
95     }\r
96 };