Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Concise Guide to Dojo ISBN: 978-0-470-45202-8
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Concise Guide to Dojo ISBN: 978-0-470-45202-8
This is the forum to discuss the Wrox book Concise Guide to Dojo by Leslie M. Orchard; ISBN: 9780470452028
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Concise Guide to Dojo ISBN: 978-0-470-45202-8 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 31st, 2009, 01:57 PM
jminatel's Avatar
Wrox Staff
Points: 17,437, Level: 57
Points: 17,437, Level: 57 Points: 17,437, Level: 57 Points: 17,437, Level: 57
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2003
Location: Indianapolis, IN, USA.
Posts: 1,933
Thanks: 57
Thanked 128 Times in 97 Posts
Default Article: Using Dojo to React to Page Load and Unload

This article is exerpted from chapter 3 "Handling Events" of the Wrox book The Concise Guide to Dojo by Leslie Michael Orchard and is reused by permission of the publisher. This may not be reused without publisher permission.
Reacting to Page Load and Unload

Reacting to the successful load and unload of a page are two basic event hooks that are the most convenient in setting up and tearing down a user interface. Dojo offers two methods to allow registration of handlers on these events, named dojo.addOnLoad() and dojo.addOnUnload(), used like so:
javascript Code:
dojo.addOnLoad(function() {
console.log("** Inline onload handler fired!");
});
 
dojo.addOnUnload(function() {
alert("** Inline onunload handler fired!");
});
If you haven’t before, now would be a good time to install Firebug (http://getfirebug.com) - or for all other browsers, ensure that isDebug is set to true in your djConfig. This will ensure that you see the output of these console.log() statements, which are used throughout this chapter to illustrate some of the less visible aspects of Dojo’s event handling.
Each of the inline anonymous functions in the preceding code will be called when the page loads up and is navigated away from, respectively. Using console.log() in the page load handler will cause a message to appear either in your Firebug log, or in the on-page debug log supplied by the version of Firebug Lite that comes with Dojo.
The page unload handler opts for an alert(), because it temporarily interrupts the transition to the next page—a console.log() statement’s output would be lost before you could see it as the browser loads the next page. Once you understand how this works, you may want to comment out this alert(), mainly because it makes each page reload a bit tedious as you play with the code.
Another way to register handlers for page load and unload is to use object methods, like this:
javascript Code:
dojo.declare('decafbad.eventsanim.events.pageHandlers', null, {
handleOnLoad: function() {
// this == handlers
console.log("** Object onload handler fired!");
},
handleOnUnload: function() {
// this == handlers
//alert("** Object onunload handler fired!");
console.log("** Object onunload handler fired!");
}
});
var handlers = new decafbad.eventsanim.events.pageHandlers();
 
dojo.addOnLoad(handlers, 'handleOnLoad');
dojo.addOnUnload(handlers, 'handleOnUnload');
Note that in this case, the context for this within these functions is automatically set to a reference to handlers, thus keeping them in their original object context.

The window.onload Problem
Something very interesting about dojo.addOnLoad() is that it fires registered handlers after the successful load of the DOM, Dojo, and its initial batch of dojo.require() statements—but before all the CSS and images included on the page have loaded in.
This is important, because a simple window.onload handler fires after every referenced asset on the page has arrived.
The distinction is useful, because it allows you to get page initialization done earlier. Widgets can be constructed, events wired up, and page changes applied before more visual styling takes effect—thus hopefully preempting any confusing flashes of content or other ugly intermediate setup artifacts that appear while long-loading images roll in.
The other thing worth reinforcing is that dojo.addOnLoad() fires after Dojo has loaded.
This is important because Dojo may still be finishing up some loading tasks even after the native window.onload event has fired. Dojo may still be working on resolving a few last remaining dojo.require() statements, or any number of other things. For this reason dojo.addOnLoad() is really a replacement for window.onload in the Dojo context.
__________________
Jim Minatel
Associate Publisher, WROX - A Wiley Brand
Blog: http://p2p.wrox.com/content/blogs/jminatel
Wrox on Twitter: http://twitter.com/wrox
Did someone here help you? Click on their post!

Last edited by jminatel; January 31st, 2009 at 02:19 PM.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in BLL.Article.Article.cs drohm BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 August 14th, 2006 09:56 AM
Page.Unload Personalization Saving wewald BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 3 January 17th, 2006 09:38 AM
Load and Unload Forms In Vb.net gibran VB.NET 1 December 1st, 2004 03:38 AM
Generate a random article when page loads morpheus SQL Server ASP 1 October 28th, 2003 09:45 AM



All times are GMT -4. The time now is 03:06 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.