Dojo and the onload event

2019-08-10 12:18发布

问题:

We have a mess of legacy pages. And we're trying to inject dojo into them. We're finding, however, that any binds to a "load" event, such as

window.addEventListener("load", handler)

fail to execute.

I read a couple of blog posts from 2006 that suggests that dojo is doing something to the load event, but they're old posts and not sure if they're still relevant.

Changing the binding to use dojo/ready works, but there is alot of code and we were hoping not to have to overhaul it all.

Is it not possible to use window.onload with dojo?

回答1:

It should work just fine, so the following should work:

window.addEventListener("load", function() {
  console.log("Handled by using an event listener on the onload event");
});

window.onload = function() {
  console.log("Handled by using the onload event");  
};

Even Dojo has its own module to wait until the DOM is loaded:

require([ "dojo/domReady!" ], function() {
   console.log("Handled by using the dojo/domReady module"); 
});

If you want to work with AMD and load the modules you need, you probably want to work with the dojo/domReady! module.

However, if you're working with declarative markup for widgets, then you will have to wait until these are parsed as well, which is something that takes places after the DOM is loaded, and that's where the dojo/ready module is useful for.

The dojo/ready module not only waits until the DOM is loaded, but also waits until all declarative markup is parsed into widgets.