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