]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * Utility class for setting/reading values from browser cookies.\r | |
3 | * Values can be written using the {@link #set} method.\r | |
4 | * Values can be read using the {@link #get} method.\r | |
5 | * A cookie can be invalidated on the client machine using the {@link #clear} method.\r | |
6 | */\r | |
7 | Ext.define('Ext.util.Cookies', {\r | |
8 | singleton: true,\r | |
9 | \r | |
10 | /**\r | |
11 | * Creates a cookie with the specified name and value. Additional settings for the cookie may be optionally specified\r | |
12 | * (for example: expiration, access restriction, SSL).\r | |
13 | * @param {String} name The name of the cookie to set.\r | |
14 | * @param {Object} value The value to set for the cookie.\r | |
15 | * @param {Object} [expires] Specify an expiration date the cookie is to persist until. Note that the specified Date\r | |
16 | * object will be converted to Greenwich Mean Time (GMT).\r | |
17 | * @param {String} [path] Setting a path on the cookie restricts access to pages that match that path. Defaults to all\r | |
18 | * pages ('/').\r | |
19 | * @param {String} [domain] Setting a domain restricts access to pages on a given domain (typically used to allow\r | |
20 | * cookie access across subdomains). For example, "sencha.com" will create a cookie that can be accessed from any\r | |
21 | * subdomain of sencha.com, including www.sencha.com, support.sencha.com, etc.\r | |
22 | * @param {Boolean} [secure] Specify true to indicate that the cookie should only be accessible via SSL on a page\r | |
23 | * using the HTTPS protocol. Defaults to false. Note that this will only work if the page calling this code uses the\r | |
24 | * HTTPS protocol, otherwise the cookie will be created with default options.\r | |
25 | */\r | |
26 | set : function(name, value){\r | |
27 | var argv = arguments,\r | |
28 | argc = arguments.length,\r | |
29 | expires = (argc > 2) ? argv[2] : null,\r | |
30 | path = (argc > 3) ? argv[3] : '/',\r | |
31 | domain = (argc > 4) ? argv[4] : null,\r | |
32 | secure = (argc > 5) ? argv[5] : false;\r | |
33 | \r | |
34 | document.cookie = name + "=" +\r | |
35 | escape(value) +\r | |
36 | ((expires === null) ? "" : ("; expires=" + expires.toUTCString())) +\r | |
37 | ((path === null) ? "" : ("; path=" + path)) +\r | |
38 | ((domain === null) ? "" : ("; domain=" + domain)) +\r | |
39 | ((secure === true) ? "; secure" : "");\r | |
40 | },\r | |
41 | \r | |
42 | /**\r | |
43 | * Retrieves cookies that are accessible by the current page. If a cookie does not exist, `get()` returns null. The\r | |
44 | * following example retrieves the cookie called "valid" and stores the String value in the variable validStatus.\r | |
45 | *\r | |
46 | * var validStatus = Ext.util.Cookies.get("valid");\r | |
47 | *\r | |
48 | * @param {String} name The name of the cookie to get\r | |
49 | * @return {Object} Returns the cookie value for the specified name;\r | |
50 | * null if the cookie name does not exist.\r | |
51 | */\r | |
52 | get : function(name) {\r | |
53 | var parts = document.cookie.split('; '),\r | |
54 | len = parts.length,\r | |
55 | item, i, ret;\r | |
56 | \r | |
57 | // In modern browsers, a cookie with an empty string will be stored:\r | |
58 | // MyName=\r | |
59 | // In older versions of IE, it will be stored as:\r | |
60 | // MyName\r | |
61 | // So here we iterate over all the parts in an attempt to match the key.\r | |
62 | for (i = 0; i < len; ++i) {\r | |
63 | item = parts[i].split('=');\r | |
64 | if (item[0] === name) {\r | |
65 | ret = item[1];\r | |
66 | return ret ? unescape(ret) : '';\r | |
67 | }\r | |
68 | }\r | |
69 | return null;\r | |
70 | },\r | |
71 | \r | |
72 | /**\r | |
73 | * Removes a cookie with the provided name from the browser\r | |
74 | * if found by setting its expiration date to sometime in the past.\r | |
75 | * @param {String} name The name of the cookie to remove\r | |
76 | * @param {String} [path] The path for the cookie.\r | |
77 | * This must be included if you included a path while setting the cookie.\r | |
78 | */\r | |
79 | clear : function(name, path){\r | |
80 | if (this.get(name)) {\r | |
81 | path = path || '/';\r | |
82 | document.cookie = name + '=' + '; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=' + path;\r | |
83 | }\r | |
84 | }\r | |
85 | });\r |