|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Event</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 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>Event</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><style> |
|
|
31 |
#contentContainer {padding:1em; background:#999966;} |
|
|
32 |
#contentContainer ul {height:0px; overflow:hidden;} |
|
|
33 |
</style> |
|
|
34 |
<div class="intro"> |
|
|
35 |
<p>As web pages get richer, they tend to get slower. One way to make your |
|
|
36 |
pages as responsive as possible is to carefully storyboard the page-load |
|
|
37 |
and page-paint processes so that the interactions most central to the |
|
|
38 |
page's purpose are enabled as early as possible. The window object's |
|
|
39 |
<code>load</code> event won't happen until the full DOM and all image data |
|
|
40 |
have loaded. Putting off script execution until after the page loads can |
|
|
41 |
be optimal for some scripts, but sometimes you won't want to wait that long |
|
|
42 |
to begin interacting with the page via script.</p> |
|
|
43 |
|
|
|
44 |
<p>The Event Utility gives you three additional interesting moments that |
|
|
45 |
occur during a page's load process: <a href="domready.html"><code>available</code>, |
|
|
46 |
<code>contentready</code>, and <code>domready</code></a>.</p> |
|
|
47 |
|
|
|
48 |
<p>In the example box below, all three events are all in use. A |
|
|
49 |
<code><div></code> (with a green background) loads; it has 100 children; |
|
|
50 |
one of those children is an arbitrarily large image that will take awhile to |
|
|
51 |
load.</p> |
|
|
52 |
|
|
|
53 |
<p> |
|
|
54 |
<strong>Note:</strong> The order of the events isn't guaranteed because |
|
|
55 |
<code>available</code> and <code>contentready</code> are fired from a polling mechanism, and |
|
|
56 |
not all browsers support a native event to signal <code>domready</code>, so that |
|
|
57 |
will fall back to a timer as well. Using these events, you can trust |
|
|
58 |
the state of the DOM per subscription, but don't expect strict ordering |
|
|
59 |
between events. |
|
|
60 |
</p> |
|
|
61 |
|
|
|
62 |
<p><strong>Internet Explorer note:</strong> It isn't always safe to modify |
|
|
63 |
content during the available/contentready until after the <code>domready</code> |
|
|
64 |
moment.</p> |
|
|
65 |
</div> |
|
|
66 |
|
|
|
67 |
<div class="example yui3-skin-sam"> |
|
|
68 |
<!-- include event dependencies --> |
|
|
69 |
<script src="../build/oop/oop-min.js"></script> |
|
|
70 |
<script src="../build/event-custom-base/event-custom-base-min.js"></script> |
|
|
71 |
<script src="../build/event-custom-complex/event-custom-complex-min.js"></script> |
|
|
72 |
<script src="../build/intl/intl-min.js"></script> |
|
|
73 |
<script src="../build/dom-core/dom-core-min.js"></script> |
|
|
74 |
<script src="../build/dom-base/dom-base-min.js"></script> |
|
|
75 |
<script src="../build/selector-native/oop-min.js"></script> |
|
|
76 |
<script src="../build/node-core/node-core-min.js"></script> |
|
|
77 |
<script src="../build/node-base/node-base-min.js"></script> |
|
|
78 |
<script src="../build/event-base/event-base-min.js"></script> |
|
|
79 |
<div id="contentContainer"> |
|
|
80 |
<div id="demo"></div> |
|
|
81 |
|
|
|
82 |
<!--a ul with an arbitrarily large number of children:--> |
|
|
83 |
<ul> |
|
|
84 |
|
|
|
85 |
</ul> |
|
|
86 |
|
|
|
87 |
<img src="../assets/event/uluru.jpg" width="500" alt="Uluru" id="image" /> |
|
|
88 |
|
|
|
89 |
</div> |
|
|
90 |
|
|
|
91 |
<script> |
|
|
92 |
YUI().use('*', function (Y) { |
|
|
93 |
var results = Y.one('#demo'); |
|
|
94 |
|
|
|
95 |
//assign page load handler: |
|
|
96 |
Y.on("load", function () { |
|
|
97 |
results.append("<p>The window load event fired. The page and all of its image data, including the large image of Uluru, has completed loading.</p>"); |
|
|
98 |
}, Y.config.win); |
|
|
99 |
|
|
|
100 |
//assign domready handler: |
|
|
101 |
Y.on("domready", function () { |
|
|
102 |
results.append("<p>The 'domready' event fired. The DOM is now safe to modify via script.</p>"); |
|
|
103 |
}); |
|
|
104 |
|
|
|
105 |
//assign 'contentready' handler: |
|
|
106 |
Y.on("contentready", function () { |
|
|
107 |
results.append("<p>The 'contentready' event fired for the element 'contentContainer'. That element and all of its children are present in the DOM.</p>"); |
|
|
108 |
}, "#contentContainer"); |
|
|
109 |
|
|
|
110 |
//assign 'available' handler: |
|
|
111 |
Y.on("available", function () { |
|
|
112 |
results.append("<p>The 'available' event fired on the element 'contentContainer'. That element is present in the DOM.</p>"); |
|
|
113 |
}, "#contentContainer"); |
|
|
114 |
|
|
|
115 |
results.append("<p>As the page loads, you'll see the 'available', 'contentready', 'domready', and window load events logged here as they fire in sequence.</p>"); |
|
|
116 |
|
|
|
117 |
}); |
|
|
118 |
</script> |
|
|
119 |
|
|
|
120 |
</div> |
|
|
121 |
|
|
|
122 |
<h2 class="first" id="source-code-for-this-example">Source Code for This Example:</h2> |
|
|
123 |
|
|
|
124 |
<h3 id="markup">Markup:</h3> |
|
|
125 |
|
|
|
126 |
<p>The markup used to create the DOM is very simple, consisting of a <code><div></code> that holds a <code><ul></code> with 100 child <code><li></code>s and a single ~3MB image. The <code><ul></code> will take a little time to load, and the image (loading over the internet) will take a few seconds to load even on a fast connection. That should allow us to see in the browser console some time deltas between when the <code><div></code> whose ID is <code>contentContainer</code> becomes available, when its children (those 100 <code><li></code>s) are ready, when the DOM is ready (including all the navigation elements on the page), and lastly when the page loads (ie, when that ~3MB image is fully loaded). </p> |
|
|
127 |
|
|
|
128 |
<pre class="code prettyprint"><div id="contentContainer"> |
|
|
129 |
|
|
|
130 |
<!--a ul with an arbitrarily large number of children:--> |
|
|
131 |
<ul> |
|
|
132 |
<li>...</li> |
|
|
133 |
<!--...100 more of these--> |
|
|
134 |
</ul> |
|
|
135 |
|
|
|
136 |
<img src="http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/large/uluru.jpg" width="500" alt="Uluru" id="image" /> |
|
|
137 |
|
|
|
138 |
</div></pre> |
|
|
139 |
|
|
|
140 |
|
|
|
141 |
<h3 id="css">CSS:</h3> |
|
|
142 |
|
|
|
143 |
<p>The CSS colors the contentContainer element and hides the big list to keep the example more compact.</p> |
|
|
144 |
|
|
|
145 |
<pre class="code prettyprint"><style type="text/css"> |
|
|
146 |
#contentContainer {padding:1em; background:#999966;} |
|
|
147 |
#contentContainer ul {height:0px; overflow:hidden;} |
|
|
148 |
</style></pre> |
|
|
149 |
|
|
|
150 |
|
|
|
151 |
<h3 id="javascript">JavaScript:</h3> |
|
|
152 |
<p>In the script, we subscribe to the four events in which we're interested and, in each case, log a message to the console to express the timing of the events as they fire.</p> |
|
|
153 |
|
|
|
154 |
<pre class="code prettyprint">YUI().use('*', function(Y) { |
|
|
155 |
var results = Y.one('#demo'); |
|
|
156 |
|
|
|
157 |
//assign page load handler: |
|
|
158 |
Y.on("load", function () { |
|
|
159 |
results.append("<p>The window load event fired. The page and all of its image data, including the large image of Uluru, has completed loading.</p>"); |
|
|
160 |
}, Y.config.win); |
|
|
161 |
|
|
|
162 |
//assign domready handler: |
|
|
163 |
Y.on("domready", function () { |
|
|
164 |
results.append("<p>The DOMContentLoaded event fired. The DOM is now safe to modify via script.</p>"); |
|
|
165 |
}); |
|
|
166 |
|
|
|
167 |
//assign 'contentready' handler: |
|
|
168 |
Y.on("contentready", function () { |
|
|
169 |
results.append("<p>The 'contentready' event fired for the element 'contentContainer'. That element and all of its children are present in the DOM.</p>"); |
|
|
170 |
}, "#contentContainer"); |
|
|
171 |
|
|
|
172 |
//assign 'available' handler: |
|
|
173 |
Y.on("available", function () { |
|
|
174 |
results.append("<p>The 'available' event fired on the element 'contentContainer'. That element is present in the DOM.</p>"); |
|
|
175 |
}, "#contentContainer"); |
|
|
176 |
|
|
|
177 |
results.append("<p>As the page loads, you'll see the 'available', 'contentready', 'domready', and window load events logged here as they fire in sequence.</p>"); |
|
|
178 |
|
|
|
179 |
});</pre> |
|
|
180 |
|
|
|
181 |
</div> |
|
|
182 |
</div> |
|
|
183 |
</div> |
|
|
184 |
|
|
|
185 |
<div class="yui3-u-1-4"> |
|
|
186 |
<div class="sidebar"> |
|
|
187 |
|
|
|
188 |
<div id="toc" class="sidebox"> |
|
|
189 |
<div class="hd"> |
|
|
190 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
191 |
</div> |
|
|
192 |
|
|
|
193 |
<div class="bd"> |
|
|
194 |
<ul class="toc"> |
|
|
195 |
<li> |
|
|
196 |
<a href="#source-code-for-this-example">Source Code for This Example:</a> |
|
|
197 |
<ul class="toc"> |
|
|
198 |
<li> |
|
|
199 |
<a href="#markup">Markup:</a> |
|
|
200 |
</li> |
|
|
201 |
<li> |
|
|
202 |
<a href="#css">CSS:</a> |
|
|
203 |
</li> |
|
|
204 |
<li> |
|
|
205 |
<a href="#javascript">JavaScript:</a> |
|
|
206 |
</li> |
|
|
207 |
</ul> |
|
|
208 |
</li> |
|
|
209 |
</ul> |
|
|
210 |
</div> |
|
|
211 |
</div> |
|
|
212 |
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
<div class="sidebox"> |
|
|
216 |
<div class="hd"> |
|
|
217 |
<h2 class="no-toc">Examples</h2> |
|
|
218 |
</div> |
|
|
219 |
|
|
|
220 |
<div class="bd"> |
|
|
221 |
<ul class="examples"> |
|
|
222 |
|
|
|
223 |
|
|
|
224 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
225 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
226 |
</li> |
|
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
231 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
232 |
</li> |
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
237 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
238 |
</li> |
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
|
|
|
251 |
</ul> |
|
|
252 |
</div> |
|
|
253 |
</div> |
|
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
<div class="sidebox"> |
|
|
258 |
<div class="hd"> |
|
|
259 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
260 |
</div> |
|
|
261 |
|
|
|
262 |
<div class="bd"> |
|
|
263 |
<ul class="examples"> |
|
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
|
271 |
|
|
|
272 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
273 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
274 |
</li> |
|
|
275 |
|
|
|
276 |
|
|
|
277 |
|
|
|
278 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
279 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
280 |
</li> |
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
<li data-description="Example Photo Browser application."> |
|
|
285 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
286 |
</li> |
|
|
287 |
|
|
|
288 |
|
|
|
289 |
|
|
|
290 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
291 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
292 |
</li> |
|
|
293 |
|
|
|
294 |
|
|
|
295 |
|
|
|
296 |
<li data-description="Use IO to request data over HTTP."> |
|
|
297 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
298 |
</li> |
|
|
299 |
|
|
|
300 |
|
|
|
301 |
</ul> |
|
|
302 |
</div> |
|
|
303 |
</div> |
|
|
304 |
|
|
|
305 |
</div> |
|
|
306 |
</div> |
|
|
307 |
</div> |
|
|
308 |
</div> |
|
|
309 |
|
|
|
310 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
311 |
<script>prettyPrint();</script> |
|
|
312 |
|
|
|
313 |
<script> |
|
|
314 |
YUI.Env.Tests = { |
|
|
315 |
examples: [], |
|
|
316 |
project: '../assets', |
|
|
317 |
assets: '../assets/event', |
|
|
318 |
name: 'event', |
|
|
319 |
title: 'Event', |
|
|
320 |
newWindow: '', |
|
|
321 |
auto: false |
|
|
322 |
}; |
|
|
323 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
324 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
325 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
326 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
327 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
328 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
329 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
330 |
YUI.Env.Tests.examples.push('get'); |
|
|
331 |
|
|
|
332 |
</script> |
|
|
333 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
334 |
|
|
|
335 |
|
|
|
336 |
|
|
|
337 |
</body> |
|
|
338 |
</html> |