Dojo - dojo.addOnLoad vs. body onload and window.onload
Posted in dojo, ajax Fri, 14 Jul 2006 04:57:00 GMT
When using Dojo Toolkit, you should avoid using <body onload="..."> or window.onload (make sure to check external scripts you use as well). This is because Dojo does it's own window.onload and doing it yourself can interfere with Dojo's initialization routines (including DOM rewriting). Dojo has provided dojo.addOnLoad for this which allows you to load functions after Dojo has finished its own initilization.
To use dojo.addOnLoad you need to pass a function which can be in either of the following formats:
dojo.addOnLoad( myinit );
dojo.addOnLoad( function() { myinit(); });
You cannot, however, use a function call like the following since Dojo is expecting an object, not a function call.
dojo.addOnLoad( myinit() );
I discussed using dojo.addOnLoad in a previous entry about setting Dojo's combobox default value but thought it worthwhile to have a separate entry since this issue continues to come up.
When using dojo.addOnLoad(init), I sometimes get the following output on the web page: FATAL: bad srcObj for srcFunc: onkeyup
Here’s the init function:
function init() { var searchField = dojo.byId(‘searchField’);
dojo.event.connect(searchField,’onkeyup’,’searchFieldChanged’);
}//init
But when use the <body onLoad=”init();”> instead of dojo.addOnLoad, I do not have this problem.
Using dojo 0.3 Testing on Mac OSX Firefox 1.5.0.6
Error is more frequent on a dial-up connection, but not with the body onload.
Using Windows XP, I can tell you that works with Firefox 2.0 but not with Internet Explorer 6.
Thanks for this post. This saved me a good deal of debugging time.