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

J

John J. Lee

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
 
J

John J. Lee

(Oops, I forgot about my post...)

Jasen Betts said:
??? what's the problem again? defer causes scripts to load or execute late
onload should be sufficient for the loading of the document.

The problem with using onload is described in the page I referenced in
my original post here. That page attempts to (and claims to succeed)
find a way of initialisation code at the earliest opportunity:
ie. when the DOM is loaded, but before objects such as images are
loaded (but without filling the HTML document with onfoo="bar"
callback bindings).

you could use callbacks to detect the loading of statically loaded scripts
I expect they also support onload, dynamically loaded scripts seem to
support onload.

The pages I referenced discuss, and reject, at least one way of making
use of script elements to solve the problem. IIRC, the problems are
related to cross-browser compatibility. What specifically did you
have in mind?


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top