/ext-4.1.0_b3/docs/source/Cookies.html
HTML | 111 lines | 105 code | 6 blank | 0 comment | 0 complexity | fa9823618dd17d269ddc8403aafd713f MD5 | raw file
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> * Utility class for setting/reading values from browser cookies.
20 * Values can be written using the {@link #set} method.
21 * Values can be read using the {@link #get} method.
22 * A cookie can be invalidated on the client machine using the {@link #clear} method.
23 */
24Ext.define('Ext.util.Cookies', {
25 singleton: true,
26
27<span id='Ext-util-Cookies-method-set'> /**
28</span> * Creates a cookie with the specified name and value. Additional settings for the cookie may be optionally specified
29 * (for example: expiration, access restriction, SSL).
30 * @param {String} name The name of the cookie to set.
31 * @param {Object} value The value to set for the cookie.
32 * @param {Object} [expires] Specify an expiration date the cookie is to persist until. Note that the specified Date
33 * object will be converted to Greenwich Mean Time (GMT).
34 * @param {String} [path] Setting a path on the cookie restricts access to pages that match that path. Defaults to all
35 * pages ('/').
36 * @param {String} [domain] Setting a domain restricts access to pages on a given domain (typically used to allow
37 * cookie access across subdomains). For example, "sencha.com" will create a cookie that can be accessed from any
38 * subdomain of sencha.com, including www.sencha.com, support.sencha.com, etc.
39 * @param {Boolean} [secure] Specify true to indicate that the cookie should only be accessible via SSL on a page
40 * using the HTTPS protocol. Defaults to false. Note that this will only work if the page calling this code uses the
41 * HTTPS protocol, otherwise the cookie will be created with default options.
42 */
43 set : function(name, value){
44 var argv = arguments,
45 argc = arguments.length,
46 expires = (argc > 2) ? argv[2] : null,
47 path = (argc > 3) ? argv[3] : '/',
48 domain = (argc > 4) ? argv[4] : null,
49 secure = (argc > 5) ? argv[5] : false;
50
51 document.cookie = name + "=" + escape(value) + ((expires === null) ? "" : ("; expires=" + expires.toGMTString())) + ((path === null) ? "" : ("; path=" + path)) + ((domain === null) ? "" : ("; domain=" + domain)) + ((secure === true) ? "; secure" : "");
52 },
53
54<span id='Ext-util-Cookies-method-get'> /**
55</span> * Retrieves cookies that are accessible by the current page. If a cookie does not exist, `get()` returns null. The
56 * following example retrieves the cookie called "valid" and stores the String value in the variable validStatus.
57 *
58 * var validStatus = Ext.util.Cookies.get("valid");
59 *
60 * @param {String} name The name of the cookie to get
61 * @return {Object} Returns the cookie value for the specified name;
62 * null if the cookie name does not exist.
63 */
64 get : function(name){
65 var arg = name + "=",
66 alen = arg.length,
67 clen = document.cookie.length,
68 i = 0,
69 j = 0;
70
71 while(i < clen){
72 j = i + alen;
73 if(document.cookie.substring(i, j) == arg){
74 return this.getCookieVal(j);
75 }
76 i = document.cookie.indexOf(" ", i) + 1;
77 if(i === 0){
78 break;
79 }
80 }
81 return null;
82 },
83
84<span id='Ext-util-Cookies-method-clear'> /**
85</span> * 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 * @param {String} [path] The path for the cookie.
89 * This must be included if you included a path while setting the cookie.
90 */
91 clear : function(name, path){
92 if(this.get(name)){
93 path = path || '/';
94 document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=' + path;
95 }
96 },
97
98<span id='Ext-util-Cookies-method-getCookieVal'> /**
99</span> * @private
100 */
101 getCookieVal : function(offset){
102 var endstr = document.cookie.indexOf(";", offset);
103 if(endstr == -1){
104 endstr = document.cookie.length;
105 }
106 return unescape(document.cookie.substring(offset, endstr));
107 }
108});
109</pre>
110</body>
111</html>