- The Book of Dojo
- The Dojo Book, 0.4
- Part 1: "Introduction"
- Part 2: "Out of the Box" Dojo
- Part 3: "The Dojo Programming Model"
- Part 4: "More on Widgets"
- Part 5: "Connecting the pieces"
- Part 6: "Customizing Dojo Builds for Better Performance"
- Part 7: "Utilities"
- Part 8: "Internationalization and Accessiblity"
- Part 9: "Dojo Community"
- Part 10: "Fresh From The Shed" Dojo
- BookWriting
- Glossary
Page Load / Unload
Submitted by Carla on Thu, 12/14/2006 - 06:14.
Often you will want to schedule some code to run on page load. Traditionally, this is done like
window.onLoad = ...;
or perhaps
However, that won't work for Dojo, because Dojo needs to override window load and unload. So, you should do this:
function init(){
...
}
dojo.addOnLoad(init);
function cleanup(){
...
}
dojo.addOnUnload(cleanup);Just like the normal dojo.event.connect() call, addOnLoad() and addOnUnload() can be called multiple times without overwriting the previous values, so you don't have to worry about one piece of Javascript code affecting another.
The line dojo.addOnLoad(init); tells Dojo to call the init function when it has finished loading correctly. This is very important! If the init
function was called before Dojo has finished parsing the HTML then widget objects would not have been instantiated and so would not exist at that point in time -
causing a nasty error.
