As web pages get richer, they tend to get slower. One way to make your pages as responsive as possible is to carefully storyboard the page-load and page-paint processes so that the interactions most central to the page's purpose are enabled as early as possible. The window object's load
event won't happen until the full DOM and all image data have loaded. Putting off script execution until after the page loads can be optimal for some scripts, but sometimes you won't want to wait that long to make something draggable, to load a Calendar Control, to draw a Tabview, and so on.
The YUI Event Utility gives you three additional interesting moments that occur during a page's load process:
onAvailable:
onAvailable
targets a single element and fires when that element is available (when it responds to document.getElementById()
) — but you can't count on the element's children having been loaded at this point.onContentReady:
When you care about not just your target element but its children as well, use onContentReady
. This method will tell you that your target element and all of its children are present in the DOM. onDOMReady:
Some DOM scripting operations cannot be performed safely until the page's entire DOM has loaded. onDOMReady
will let you know that the DOM is fully loaded and ready for you to modify via script.In the example box below, onAvailable
, onContentReady
and onDOMReady
are all in use. A <div>
(with a green background) loads; it has 100 chidren; one of those children is an arbitrarily large image that will take awhile to load. Keep an eye on the logger console at right. You'll see that the <div>
(1) becomes available, (2) its content becomes ready (after all of its 100 children have loaded), (3) the DOM becomes ready, and finally (4) the page loads. (Note: In the Logger console, newer messages appear at the top.)
The markup used to create the DOM is very simple, consisting of a <div>
that holds a <ul>
with 100 child <li>
s and a single ~3MB image. The <ul>
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 Logger console some time deltas between when the <div>
whose ID is contentContainer
becomes available, when its children (those 100 <li>
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).
1 <div id="contentContainer"> 2 3 <!--a ul with an arbitrarily large number of children:--> 4 <ul> 5 <li>...</li> 6 <!--...100 more of these--> 7 </ul> 8 9 <img src="http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/large/uluru.jpg" width="500" alt="Uluru" id="image" /> 10 11 </div> view plain | print | ?
The CSS colors the contentContainer element and hides the big list to keep the example more compact.
1 <style> 2 #contentContainer {padding:1em; background:#999966;} 3 #contentContainer ul {height:0px; overflow:hidden;} 4 </style> view plain | print | ?
In the script, we create an object called Timing
within the example
namespace. That object contains our event handlers. The handlers log a message about each event to the Logger console.
1 YAHOO.example.Timing = function() { 2 3 //create shortcut for YAHOO.util.Event: 4 var e = YAHOO.util.Event; 5 6 //the returned object here will be assigned 7 //to YAHOO.example.Timing and its members will 8 //then be publicly available: 9 return { 10 11 init: function() { 12 13 //assign page load handler: 14 e.on(window, "load", this.fnLoadHandler, "The window.onload event fired. The page and all of its image data, including the large image of Uluru, has completed loading."); 15 16 //assign onDOMReady handler: 17 e.onDOMReady(this.fnHandler, "The onDOMReady event fired. The DOM is now safe to modify via script."); 18 19 //assign onContentReady handler: 20 e.onContentReady("contentContainer", this.fnHandler, "The onContentReady event fired for the element 'contentContainer'. That element and all of its children are present in the DOM."); 21 22 //assign onAvailable handler: 23 e.onAvailable("contentContainer", this.fnHandler, "The onAvailable event fired on the element 'contentContainer'. That element is present in the DOM."); 24 25 }, 26 27 //we'll use this handler for onAvailable, onContentReady, 28 //and onDOMReady: 29 fnHandler: function(message) { 30 //onDOMReady uses the Custom Event signature, with the object 31 //passed in as the third argument: 32 if(arguments.length > 2) { 33 message = arguments[2]; 34 } 35 YAHOO.log(message, "info", "example"); 36 37 }, 38 39 //we'll use this handler for the page load event: 40 fnLoadHandler: function(oEvent, message) { 41 YAHOO.log(message, "info", "example"); 42 } 43 44 } 45 46 }(); 47 48 //initialize the example: 49 YAHOO.example.Timing.init(); view plain | print | ?
INFO30ms (+0) 10:48:13 PM:example
The onContentReady event fired for the element 'contentContainer'. That element and all of its children are present in the DOM.
INFO30ms (+30) 10:48:13 PM:example
The onAvailable event fired on the element 'contentContainer'. That element is present in the DOM.
INFO0ms (+0) 10:48:13 PM:global
Logger initialized
Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings