|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Subcookie Example</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Subcookie Example</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>This example shows how to get and set subcookies as well as using conversion functions when retrieving subcookie values.</p> |
|
|
30 |
</div> |
|
|
31 |
|
|
|
32 |
<div class="example yui3-skin-sam"> |
|
|
33 |
<pre id="results"></pre> |
|
|
34 |
|
|
|
35 |
<script> |
|
|
36 |
YUI().use('cookie', 'node', function (Y) { |
|
|
37 |
|
|
|
38 |
var results = Y.one('#results'), |
|
|
39 |
log = function(str) { |
|
|
40 |
results.append(str + '<br>'); |
|
|
41 |
}; |
|
|
42 |
|
|
|
43 |
//set subcookie values |
|
|
44 |
Y.Cookie.setSub("example", "name", "Yahoo!"); |
|
|
45 |
Y.Cookie.setSub("example", "today", (new Date()).toString()); |
|
|
46 |
Y.Cookie.setSub("example", "count", Math.round(Math.random() * 30)); |
|
|
47 |
|
|
|
48 |
//get subcookie values |
|
|
49 |
var name = Y.Cookie.getSub("example", "name"); |
|
|
50 |
var today = Y.Cookie.getSub("example", "today", function (value) { |
|
|
51 |
return new Date(value); |
|
|
52 |
}); |
|
|
53 |
var count = Y.Cookie.getSub("example", "count", Number); |
|
|
54 |
|
|
|
55 |
log("The subcookie 'name' is '" + name + "' (" + (typeof name) + ")"); |
|
|
56 |
log("The subcookie 'today' is '" + today + "' (" + (typeof today) + ")"); |
|
|
57 |
log("The subcookie 'count' is '" + count + "' (" + (typeof count) + ")"); |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
}); |
|
|
61 |
</script> |
|
|
62 |
|
|
|
63 |
</div> |
|
|
64 |
|
|
|
65 |
<h2>Description</h2> |
|
|
66 |
|
|
|
67 |
<p>The first three lines attempt to read the values stored in subcookies of the "example" cookie:</p> |
|
|
68 |
|
|
|
69 |
<pre class="code prettyprint">var name = Y.Cookie.getSub("example", "name"); |
|
|
70 |
var today = Y.Cookie.getSub("example", "today", function (value) { |
|
|
71 |
return new Date(value); |
|
|
72 |
}); |
|
|
73 |
var count = Y.Cookie.getSub("example", "count", Number);</pre> |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
<p>The "name" subcookie stores a string so it is retrieved without specifying a third argument.</p> |
|
|
77 |
|
|
|
78 |
<p>The "today" subcookie stores a date string, which should be converted to a <code>Date</code> object upon retrieval; the third argument of <code>getSub()</code> is specified as a custom function that will convert the returned value into a <code>Date</code> object.</p> |
|
|
79 |
|
|
|
80 |
<p>The "count" subcookie contains a number and is converted to an actual JavaScript number by passing in the native <code>Number</code> function.</p> |
|
|
81 |
|
|
|
82 |
<p>If any of these subcookies don't exist, <code>getSub()</code> returns <code>null</code>. This should be the case the first time you run the example.</p> |
|
|
83 |
|
|
|
84 |
<p>The retrieved values are shown in the browser console.</p> |
|
|
85 |
|
|
|
86 |
<p>After that, new values are assigned to the various subcookies:</p> |
|
|
87 |
|
|
|
88 |
<pre class="code prettyprint">Y.Cookie.setSub("example", "name", "Yahoo!"); |
|
|
89 |
Y.Cookie.setSub("example", "today", (new Date()).toString()); |
|
|
90 |
Y.Cookie.setSub("example", "count", Math.round(Math.random() * 30));</pre> |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
<p>The "name" subcookie is set to "Yahoo!", the "today" subcookie is set to the value of a new <code>Date</code> object as a string, and the "count" subcookie is filled with a random number. The next time you run the example, the subcookies should have these values.</p> |
|
|
94 |
|
|
|
95 |
<h2>Complete Example Source</h2> |
|
|
96 |
|
|
|
97 |
<pre class="code prettyprint"><pre id="results"></pre> |
|
|
98 |
|
|
|
99 |
<script> |
|
|
100 |
YUI().use('cookie', 'node', function (Y) { |
|
|
101 |
|
|
|
102 |
var results = Y.one('#results'), |
|
|
103 |
log = function(str) { |
|
|
104 |
results.append(str + '<br>'); |
|
|
105 |
}; |
|
|
106 |
|
|
|
107 |
//set subcookie values |
|
|
108 |
Y.Cookie.setSub("example", "name", "Yahoo!"); |
|
|
109 |
Y.Cookie.setSub("example", "today", (new Date()).toString()); |
|
|
110 |
Y.Cookie.setSub("example", "count", Math.round(Math.random() * 30)); |
|
|
111 |
|
|
|
112 |
//get subcookie values |
|
|
113 |
var name = Y.Cookie.getSub("example", "name"); |
|
|
114 |
var today = Y.Cookie.getSub("example", "today", function (value) { |
|
|
115 |
return new Date(value); |
|
|
116 |
}); |
|
|
117 |
var count = Y.Cookie.getSub("example", "count", Number); |
|
|
118 |
|
|
|
119 |
log("The subcookie 'name' is '" + name + "' (" + (typeof name) + ")"); |
|
|
120 |
log("The subcookie 'today' is '" + today + "' (" + (typeof today) + ")"); |
|
|
121 |
log("The subcookie 'count' is '" + count + "' (" + (typeof count) + ")"); |
|
|
122 |
|
|
|
123 |
|
|
|
124 |
}); |
|
|
125 |
</script></pre> |
|
|
126 |
|
|
|
127 |
</div> |
|
|
128 |
</div> |
|
|
129 |
</div> |
|
|
130 |
|
|
|
131 |
<div class="yui3-u-1-4"> |
|
|
132 |
<div class="sidebar"> |
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
<div class="sidebox"> |
|
|
137 |
<div class="hd"> |
|
|
138 |
<h2 class="no-toc">Examples</h2> |
|
|
139 |
</div> |
|
|
140 |
|
|
|
141 |
<div class="bd"> |
|
|
142 |
<ul class="examples"> |
|
|
143 |
|
|
|
144 |
|
|
|
145 |
<li data-description="Demonstrates basic usage of the Cookie utility for reading and writing cookies."> |
|
|
146 |
<a href="cookie-simple-example.html">Simple Cookie Example</a> |
|
|
147 |
</li> |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
<li data-description="Demonstrates using the Cookie utility to get, set and remove cookies."> |
|
|
152 |
<a href="cookie-advanced-example.html">Advanced Cookie Example</a> |
|
|
153 |
</li> |
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
<li data-description="Demonstrates using the Cookie utility to get and set subcookies."> |
|
|
158 |
<a href="cookie-subcookie-example.html">Subcookie Example</a> |
|
|
159 |
</li> |
|
|
160 |
|
|
|
161 |
|
|
|
162 |
</ul> |
|
|
163 |
</div> |
|
|
164 |
</div> |
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
</div> |
|
|
169 |
</div> |
|
|
170 |
</div> |
|
|
171 |
</div> |
|
|
172 |
|
|
|
173 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
174 |
<script>prettyPrint();</script> |
|
|
175 |
|
|
|
176 |
<script> |
|
|
177 |
YUI.Env.Tests = { |
|
|
178 |
examples: [], |
|
|
179 |
project: '../assets', |
|
|
180 |
assets: '../assets/cookie', |
|
|
181 |
name: 'cookie-subcookie-example', |
|
|
182 |
title: 'Subcookie Example', |
|
|
183 |
newWindow: '', |
|
|
184 |
auto: false |
|
|
185 |
}; |
|
|
186 |
YUI.Env.Tests.examples.push('cookie-simple-example'); |
|
|
187 |
YUI.Env.Tests.examples.push('cookie-advanced-example'); |
|
|
188 |
YUI.Env.Tests.examples.push('cookie-subcookie-example'); |
|
|
189 |
|
|
|
190 |
</script> |
|
|
191 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
</body> |
|
|
196 |
</html> |