|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Advanced Cookie 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: Advanced Cookie 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, set, and remove cookies using the YUI Cookie utility.</p> |
|
|
30 |
</div> |
|
|
31 |
|
|
|
32 |
<div class="example yui3-skin-sam"> |
|
|
33 |
<p>Click the buttons to interact with the cookie:</p> |
|
|
34 |
|
|
|
35 |
<p> |
|
|
36 |
<input type="button" value="Get Value" id="yui-cookie-btn1"> |
|
|
37 |
<input type="button" value="Set Random Value" id="yui-cookie-btn2"> |
|
|
38 |
<input type="button" value="Remove Value" id="yui-cookie-btn3"> |
|
|
39 |
</p> |
|
|
40 |
|
|
|
41 |
<pre id="results"></pre> |
|
|
42 |
|
|
|
43 |
<script> |
|
|
44 |
YUI().use('cookie', 'node', function (Y) { |
|
|
45 |
|
|
|
46 |
var pre = Y.one('#results'), |
|
|
47 |
log = function(str) { |
|
|
48 |
pre.append(str + '\n<br>'); |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
Y.one("#yui-cookie-btn1").on("click", function () { |
|
|
52 |
var value = Y.Cookie.get("example"); |
|
|
53 |
log("Cookie 'example' has a value of '" + value + "'"); |
|
|
54 |
}); |
|
|
55 |
|
|
|
56 |
Y.one("#yui-cookie-btn2").on("click", function () { |
|
|
57 |
var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000); |
|
|
58 |
Y.Cookie.set("example", newValue); |
|
|
59 |
log("Set cookie 'example' to '" + newValue + "'"); |
|
|
60 |
}); |
|
|
61 |
|
|
|
62 |
Y.one("#yui-cookie-btn3").on("click", function () { |
|
|
63 |
Y.Cookie.remove("example"); |
|
|
64 |
log("Removed cookie 'example'."); |
|
|
65 |
}); |
|
|
66 |
|
|
|
67 |
}); |
|
|
68 |
</script> |
|
|
69 |
|
|
|
70 |
</div> |
|
|
71 |
|
|
|
72 |
<h2>Description</h2> |
|
|
73 |
|
|
|
74 |
<p>This example consists of three buttons, each of which performs one of the basic cookie functions: getting a value, setting a value, and removing a value. The first button, "Get Value", retrieves the value of a cookie and logs it:</p> |
|
|
75 |
|
|
|
76 |
<pre class="code prettyprint">Y.one("#yui-cookie-btn1").on("click", function () { |
|
|
77 |
var value = Y.Cookie.get("example"); |
|
|
78 |
Y.log("Cookie 'example' has a value of '" + value + "'"); |
|
|
79 |
});</pre> |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
<p>The second button, "Set Random Value", creates a random value and sets the cookie's value equal to it:</p> |
|
|
83 |
|
|
|
84 |
<pre class="code prettyprint">Y.one("#yui-cookie-btn2").on("click", function () { |
|
|
85 |
var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000); |
|
|
86 |
Y.Cookie.set("example", newValue); |
|
|
87 |
Y.log("Set cookie 'example' to '" + newValue + "'"); |
|
|
88 |
});</pre> |
|
|
89 |
|
|
|
90 |
|
|
|
91 |
<p>After clicking this button, you can go back and click "Get Value" to see the new value that was assigned |
|
|
92 |
to the cookie (you can also check the logger output).</p> |
|
|
93 |
<p>The third button, "Remove Value", completely removes the cookie from the page:</p> |
|
|
94 |
|
|
|
95 |
<pre class="code prettyprint">Y.one("#yui-cookie-btn3").on("click", function () { |
|
|
96 |
Y.Cookie.remove("example"); |
|
|
97 |
Y.log("Removed cookie 'example'."); |
|
|
98 |
});</pre> |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
<p>When this button is clicked, it removes the cookie. If "Get Value" is clicked immediately afterwards, the value should be <code>null</code>.</p> |
|
|
102 |
|
|
|
103 |
<h2>Complete Example Source</h2> |
|
|
104 |
|
|
|
105 |
<pre class="code prettyprint"><p>Click the buttons to interact with the cookie:</p> |
|
|
106 |
|
|
|
107 |
<p> |
|
|
108 |
<input type="button" value="Get Value" id="yui-cookie-btn1"> |
|
|
109 |
<input type="button" value="Set Random Value" id="yui-cookie-btn2"> |
|
|
110 |
<input type="button" value="Remove Value" id="yui-cookie-btn3"> |
|
|
111 |
</p> |
|
|
112 |
|
|
|
113 |
<pre id="results"></pre> |
|
|
114 |
|
|
|
115 |
<script> |
|
|
116 |
YUI().use('cookie', 'node', function (Y) { |
|
|
117 |
|
|
|
118 |
var pre = Y.one('#results'), |
|
|
119 |
log = function(str) { |
|
|
120 |
pre.append(str + '\n<br>'); |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
Y.one("#yui-cookie-btn1").on("click", function () { |
|
|
124 |
var value = Y.Cookie.get("example"); |
|
|
125 |
log("Cookie 'example' has a value of '" + value + "'"); |
|
|
126 |
}); |
|
|
127 |
|
|
|
128 |
Y.one("#yui-cookie-btn2").on("click", function () { |
|
|
129 |
var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000); |
|
|
130 |
Y.Cookie.set("example", newValue); |
|
|
131 |
log("Set cookie 'example' to '" + newValue + "'"); |
|
|
132 |
}); |
|
|
133 |
|
|
|
134 |
Y.one("#yui-cookie-btn3").on("click", function () { |
|
|
135 |
Y.Cookie.remove("example"); |
|
|
136 |
log("Removed cookie 'example'."); |
|
|
137 |
}); |
|
|
138 |
|
|
|
139 |
}); |
|
|
140 |
</script></pre> |
|
|
141 |
|
|
|
142 |
</div> |
|
|
143 |
</div> |
|
|
144 |
</div> |
|
|
145 |
|
|
|
146 |
<div class="yui3-u-1-4"> |
|
|
147 |
<div class="sidebar"> |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
<div class="sidebox"> |
|
|
152 |
<div class="hd"> |
|
|
153 |
<h2 class="no-toc">Examples</h2> |
|
|
154 |
</div> |
|
|
155 |
|
|
|
156 |
<div class="bd"> |
|
|
157 |
<ul class="examples"> |
|
|
158 |
|
|
|
159 |
|
|
|
160 |
<li data-description="Demonstrates basic usage of the Cookie utility for reading and writing cookies."> |
|
|
161 |
<a href="cookie-simple-example.html">Simple Cookie Example</a> |
|
|
162 |
</li> |
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
<li data-description="Demonstrates using the Cookie utility to get, set and remove cookies."> |
|
|
167 |
<a href="cookie-advanced-example.html">Advanced Cookie Example</a> |
|
|
168 |
</li> |
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
<li data-description="Demonstrates using the Cookie utility to get and set subcookies."> |
|
|
173 |
<a href="cookie-subcookie-example.html">Subcookie Example</a> |
|
|
174 |
</li> |
|
|
175 |
|
|
|
176 |
|
|
|
177 |
</ul> |
|
|
178 |
</div> |
|
|
179 |
</div> |
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
</div> |
|
|
184 |
</div> |
|
|
185 |
</div> |
|
|
186 |
</div> |
|
|
187 |
|
|
|
188 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
189 |
<script>prettyPrint();</script> |
|
|
190 |
|
|
|
191 |
<script> |
|
|
192 |
YUI.Env.Tests = { |
|
|
193 |
examples: [], |
|
|
194 |
project: '../assets', |
|
|
195 |
assets: '../assets/cookie', |
|
|
196 |
name: 'cookie-advanced-example', |
|
|
197 |
title: 'Advanced Cookie Example', |
|
|
198 |
newWindow: '', |
|
|
199 |
auto: false |
|
|
200 |
}; |
|
|
201 |
YUI.Env.Tests.examples.push('cookie-simple-example'); |
|
|
202 |
YUI.Env.Tests.examples.push('cookie-advanced-example'); |
|
|
203 |
YUI.Env.Tests.examples.push('cookie-subcookie-example'); |
|
|
204 |
|
|
|
205 |
</script> |
|
|
206 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
</body> |
|
|
211 |
</html> |