Login Register

Page Load / Unload

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.