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