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).
The CSS colors the contentContainer element and hides the big list to keep the example more compact.
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.
Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings