Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#1126 closed defect (invalid)

addOnLoad does NOT provide functionality of body onload

Reported by: dave_nga Owned by: anonymous
Priority: high Milestone:
Component: General Version: 0.3
Keywords: Cc:
Blocked By: Blocking:

Description

Developers depend on a mechanism to execute code AFTER the WHOLE page has loaded; after the Document has been completely constructed in the browser; then and only then, can elements be reliably fetched and operated on. The suggested approach regarding this issue so far is to tie the event to a dojo element directly, wrap the function call, etc. OK, but what if the element (e.g. a DIV) is NOT a dojo element. How do I ensure that arbitrary javascript code is executed in the same manner as if I tied it to the body's onLoad event?

In Firefox, the following simple code snippet demonstrates the simple problem. Internet Explorer can also be used to demonstrate issue and has the added twist of resulting in a javascript error within Dojo code - not sure what that's about -- does not appear in firefox javascript console...

Anyway, here's the code: ================================================ <html> <body><head> <script type="text/javascript" src="/js/dojo3/dojo.js"></script> <script type="text/javascript">

function myinit() {

elem = document.getElementById('content'); alert("I would now call some operation to update the content DIV. getElementID reports that div is: " + elem);

}

var djConfig = {

baseRelativePath: "/js/dojo3/", isDebug: true

};

dojo.addOnLoad(myinit()); </script> </head> <div id="content"></div> <a href="javascript:myinit()">click here now that page has loaded</a> </body> </html> =================================================

I'm in the process of dojo'fying an existing application and only want to take advantage of the dojo calendar object, but I have some very basic ajax already in place that works, so I don't really want to retrofit my whole application with dojo at this time. Thus, I need access to the body's onLoad event...

Change History (3)

comment:1 Changed 13 years ago by bill

Resolution: invalid
Status: newclosed

Not a bug. Call

dojo.addOnLoad(myinit);

instead of

dojo.addOnLoad(myinit());

comment:2 Changed 13 years ago by guest

Thanks a lot. This works. Kind of silly of me to miss that mistake. Obviously, my code example sends the result of the function to addOnLoad, rather the function itself. Duh... was used to the eval'ish use when setting body.onload.

Thanks for your quick response.

comment:3 Changed 13 years ago by sjmiles

This is perhaps odd to note here, but this kind of problem can be avoided by using an anonymous function as argument to addOnLoad, like so:

dojo.addOnLoad(function() {
  elem = document.getElementById('content'); 
  alert("getElementID reports that div is: " + elem); 
});

I would like more Dojo standard code to use this idiom. It spares the global namespace from 'init' and leaves no confusion as to the invocation of the function (i.e. in the original example, it's perhaps non-obvious where 'init' is invoked).

Note: See TracTickets for help on using tickets.