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