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