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