Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / Cookies.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-util.Cookies'>/**
2 </span> * @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 <span id='Ext-util.Cookies-method-set'>    /**
16 </span>     * 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 (&lt;tt&gt;'/'&lt;/tt&gt;). 
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, &quot;sencha.com&quot; 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 &lt;tt&gt;false&lt;/tt&gt;. 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 &gt; 2) ? argv[2] : null,
41             path = (argc &gt; 3) ? argv[3] : '/',
42             domain = (argc &gt; 4) ? argv[4] : null,
43             secure = (argc &gt; 5) ? argv[5] : false;
44             
45         document.cookie = name + &quot;=&quot; + escape(value) + ((expires === null) ? &quot;&quot; : (&quot;; expires=&quot; + expires.toGMTString())) + ((path === null) ? &quot;&quot; : (&quot;; path=&quot; + path)) + ((domain === null) ? &quot;&quot; : (&quot;; domain=&quot; + domain)) + ((secure === true) ? &quot;; secure&quot; : &quot;&quot;);
46     },
47
48 <span id='Ext-util.Cookies-method-get'>    /**
49 </span>     * Retrieves cookies that are accessible by the current page. If a cookie
50      * does not exist, &lt;code&gt;get()&lt;/code&gt; returns &lt;tt&gt;null&lt;/tt&gt;.  The following
51      * example retrieves the cookie called &quot;valid&quot; and stores the String value
52      * in the variable &lt;tt&gt;validStatus&lt;/tt&gt;.
53      * &lt;pre&gt;&lt;code&gt;
54      * var validStatus = Ext.util.Cookies.get(&quot;valid&quot;);
55      * &lt;/code&gt;&lt;/pre&gt;
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 + &quot;=&quot;,
62             alen = arg.length,
63             clen = document.cookie.length,
64             i = 0,
65             j = 0;
66             
67         while(i &lt; clen){
68             j = i + alen;
69             if(document.cookie.substring(i, j) == arg){
70                 return this.getCookieVal(j);
71             }
72             i = document.cookie.indexOf(&quot; &quot;, i) + 1;
73             if(i === 0){
74                 break;
75             }
76         }
77         return null;
78     },
79
80 <span id='Ext-util.Cookies-method-clear'>    /**
81 </span>     * 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 <span id='Ext-util.Cookies-method-getCookieVal'>    /**
94 </span>     * @private
95      */
96     getCookieVal : function(offset){
97         var endstr = document.cookie.indexOf(&quot;;&quot;, offset);
98         if(endstr == -1){
99             endstr = document.cookie.length;
100         }
101         return unescape(document.cookie.substring(offset, endstr));
102     }
103 });
104 </pre></pre></body></html>