Categories
Etc

The Page Event Life Cycle

This is the first in a series of posts on browser-based development with Dojo. These were originally intended for the Bloglines team, but I thought I’d throw them out to a larger audience. I’ll try to keep the entries short, but no promises. Some things just need lots of explanation… Anyway, here’s the first and it’s on the event life cycle for a page.

When an HTML page loads (and unloads) a number of events fire allowing the application to do different things. Browsers tend to fire the following events:

window.onload
fires when the page has finished loading. This includes loading any external resources referenced by the HTML, like stylesheets, script, and images, both tags and images referenced by background: rules in CSS.
window.onunload
fires when the user navigates away from the page. This one is provided to give your script the ability to do stuff that needs to happen when the user leaves, like save state or cleanup outstanding XHRs. This event also tends to like to terminate execution after a set amount of time, so anything you do in here has to happen fast. Your script may not run to completion, depending on how long you’re taking and which browser you’re on. I don’t recall the limits off-hand, but they do vary.

In addition, some browsers expose a method for determining when the DOM is done being constructed, but images and such have not finished loading. Firefox calls this onDOMContentLoaded and there are hacks to get it supported on other browsers. This is really handy; when you’re pulling content from edge cached locations you don’t want to wait for all those locations to answer DNS and respond before you can get on with setting up your page and responding to user events.

Now, why wait for onLoad at all before running script? Well, the DOM is in an indeterminate state beforehand. Sure, you can setup an interval and query for things, but it’s generally safer to just wait for the browser to tell you everything is ok and then go on your way.

Dojo wraps up some of the hacks referenced above and exposes them as dojo.addOnLoad(). If you want something to happen when the browser is all set, just add it like this:

Definitely do not do the following:

This will whack any event handlers stuck onto onload (including Dojo’s) and nothing based on onLoad will work. A similar warning applies to onUnload. Don’t go trying to window.onunload = myAwesomeCleanupFunction. Dojo does some cleanup work in unload that we really want to keep around to keep IE from leaking like a sieve.

So, you may be wondering, what kinds of things would I want to dojo.addOnLoad()? How do I sync up all these things if there are dependencies between them? For Bloglines, we try to funnel everything init related through the main class for the page, which for /b/view is an instantiation of bl.page.view. bl.page.view hooks onto Dojo’s onLoad and does a whole bunch of work in bl.page.view.init. Instead of trying to hook all the independent parts of the application up to Dojo’s onLoad, we let init take care of all the lovely synchronization issues. That.. mostly works.

So with all this work going on in init, what happens to the browser? More on that and the wonderful single-threaded, message-based world of DOM development in the next installment.

Advertisements
Categories
Etc

Monitor Mounts FTW

three-panel.jpgI’ve been reading Scott Hanselman‘s series on setting up a home office with great interest, as I too am working from home with a stay-at-home wife and small child. However, after reading a recent post on the office layout, I think he could really use one of these monitor mounts. We used to use similar things back at FactSet for some of the folks with multi-monitor setups and they seemed to work quite well. Getting more stuff up off the desk is always a good thing.

For my home office, I have a MacBook Pro sitting next to a 24″ Dell flat panel, and another separate desk (with a 22″ Samsung and my 13″ printer) specifically for working on photos. I found having more than one computer on one desk cumbersome and it quickly led to an out-of-control cord and wire mess. I’m totally with him on the wired network too, gigabit is just so much faster and more reliable than wireless of any flavor, and it’s generally pretty easy and economical to run. I’m only using Cat5E at the moment, but might switch to Cat6 at some point in the future to see how it compares. I don’t have the advantage of wiring sans sheet rock, but there’s enough hackery in this house already that moving some wire around isn’t too hard.

Categories
Etc

Who you gonna call?

Ajax Ghost Busters

Saw this on Drawn! and had to share it. If only I had a  hi-res version for the wall. :) Originally posted on Animation Backgrounds.

Categories
Etc

Congrats to Dojo on 1.0!

Last night, Dojo 1.0 was tagged and released into the wild for all to play with. Massive congratulations to the entire team on this phenomenal piece of work. As has been documented before, it’s the underpinning of the new Bloglines front-end and has served as a excellent foundation upon which to build.

More in the future about how we’re using Dojo, why we chose it and how we interact with it internally.

Categories
Etc

Fresh

For those only seeing this in feed readers, blowery.org is sporting a new look today. Out with the dark, in with the light. Come check it out. :)