dojo icon

Dojo - dojo.addOnLoad vs. body onload and window.onload

Posted in , 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.

del.icio.us:Dojo - dojo.addOnLoad vs. body onload and window.onload digg:Dojo - dojo.addOnLoad vs. body onload and window.onload reddit:Dojo - dojo.addOnLoad vs. body onload and window.onload spurl:Dojo - dojo.addOnLoad vs. body onload and window.onload wists:Dojo - dojo.addOnLoad vs. body onload and window.onload simpy:Dojo - dojo.addOnLoad vs. body onload and window.onload newsvine:Dojo - dojo.addOnLoad vs. body onload and window.onload blinklist:Dojo - dojo.addOnLoad vs. body onload and window.onload furl:Dojo - dojo.addOnLoad vs. body onload and window.onload fark:Dojo - dojo.addOnLoad vs. body onload and window.onload blogmarks:Dojo - dojo.addOnLoad vs. body onload and window.onload Y!:Dojo - dojo.addOnLoad vs. body onload and window.onload smarking:Dojo - dojo.addOnLoad vs. body onload and window.onload magnolia:Dojo - dojo.addOnLoad vs. body onload and window.onload segnalo:Dojo - dojo.addOnLoad vs. body onload and window.onload

3 comments

Comments

  1. Alex said about 1 month later:

    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.

  2. FunkyBoy said 7 months later:

    Using Windows XP, I can tell you that works with Firefox 2.0 but not with Internet Explorer 6.

  3. Richard Hoberman said about 1 year later:

    Thanks for this post. This saved me a good deal of debugging time.

(leave url/email »)

   Comment Markup Help Preview comment