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