Late window.onload, defer, DOMContentLoaded and multiple source files



I was cheered to see that this guy claims to have solved the problem
with onload only firing very late:

http://dean.edwards.name/weblog/2005/09/busted/


[Before anybody suggests it, I'm aware of HTCs, but it's not always
possible to use them.]

However, "ash" comments on that page:

> The 'defer' attribute is a tricky little beast. Beware of
> dependencies between scripts when using the 'defer'
> attribute. i.e. if the 'onload' script in file2.js depends on
> functions defined in file1.js, you should avoid adding a 'defer'
> attribute to the script tag for file1.js.
>
> The problems caused are infrequent, but are easily summarized by
> saying you cannot guarantee functions will be available from a
> deferred script until after 'onload' fires. Which is what you were
> using 'defer' for in the first place.
>
> If everything's in one big 'defer' script, you should be fine
> though.
[...]

So, it's not possible to keep separate "behaviours" in separate
scripts? Different stuff like tab switching, tree view
expansion/contraction, etc. plus application-specific code, must all
be lumped together into on huge JS file? (OK, only the code actually
used by initialisation, but I can't immediately see how that makes
things much less painful for me.) Did I understand that right?

Somebody also seemed to suggest that this technique breaks
"registering" onload callbacks
(eg. http://simon.incutio.com/archive/2004/05/26/addLoadEvent), but I
haven't tried to work that one through yet.

If I do understand that right, I wonder about another approach.
Somebody else on that page suggested a timer combined with a "dummy"
element at the bottom of each page. You use a timer to poll, and when
the dummy element turns up in the DOM, that's when you do your
initialisation. Does anybody here have links to discussion /
implementation(s) of that idea?

Thanks in advance for any help,


John
.



Relevant Pages

  • Re: Causing File Download on Page Load
    ... Script code in the document head or body is executed while the document is ... So script in the onload handler executes after the document is finished loading? ... and not using a wrapper. ...
    (comp.lang.javascript)
  • Re: John Resig Video
    ... Modern browsers perform parallel downloads. ... The statement I was commenting on came at the end of a paragraph that commenced "The defer attribute tells the parser that it can continue downloading and rendering the page", so an example that does not feature the DEFER attribute seems to serve no purpose as a clarification of what you were talking about. ... As the order of execution is not specified in relation to the DEFER attribute using it on scripts for which the order of execution did matter would be a mistake. ... Adding defer attribute to a script will likely result in the ...
    (comp.lang.javascript)
  • Re: Dynamic select lists using optgroups again - without frameworks
    ... in the last script element. ... The DOM will be ready and you don't need to use window.onload (or ... Using onload is also based on the "mere assumption" that the UA ... at the time the feature was introduced, ...
    (comp.lang.javascript)
  • Re: John Resig Video
    ... Modern browsers perform parallel downloads. ... The statement I was commenting on came at the end of a paragraph that commenced "The defer attribute tells the parser that it can continue downloading and rendering the page", so an example that does not feature the DEFER attribute seems to serve no purpose as a clarification of what you were talking about. ... That fulfills the "doesn't matter when it executes" statement. ... It would be somewhat desirable to have better control over script ...
    (comp.lang.javascript)
  • Re: Build web apps in Javascript - new service open for beta
    ... You are using the onload event of two entirely different ... was attempting to add an event handler to a single element. ... means do it by script. ... Your "senior programmer" should be demoted to junior ...
    (comp.lang.javascript)