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