David said:
[snip]
document.readyState == "complete" ?
In Firefox 3.6[1].
Already in IE, Safari, and Opera.
To further explain what Matt and I are both looking for, here's an example.
Site A decides to load library script B at some point during the viewing
of a page. It's completely unknown when this might be. They might be
doing it in the head section, in the body section, or on-demand well
after the onload event has fired because of some user action.
Script B wants to know into which page state it's being loaded. Whether
or not the event has fired already.
Question: How can script B know if onload has fired without putting
demands on site A to have to set flags (because the browser itself
doesn't seem to want to).
The loader script can determine what the state of the document is in and
set the flag. What's wrong with that?
It isn't a cross-browser solution and that property is available to
the loaded script, so it doesn't need to be mirrored in a flag. The
loader script can set a load listener. The load listener sets a
flag. That's the only way to know for sure if the document is loaded.
I think we are discussing the exact same thing here.
I should have stated that document.readyState is not widely enough
implemented; it is not even implemented in official Firefox release yet.
Sounds like Matt wants to have a user-defined Loader object to load the
scripts. SOmething like:
Loader.loadScripts(srcArray, scriptLoaded);
?
1) loader script adds to window.onload "loaderWindowOnload" (that sets
loader.complete=true).
2) loader loads script(s).
3) when a script is loaded, that script checks the loader's complete flag.
4) if the complete flag is false, the loaded script may add a callback
to window.onload. Otherwise, it just calls whatever function it needed.
This solves the problem where loader.loadScripts loads 10 scripts, the
first five are loaded before window.onload fires, then the next five are
loaded after onload, but the last five want to know about onload.
If the loaded scripts are designed so that they listen for
window.onload, they will be waiting forever. This approach solves that.
The problem with the "loader sets a flag" approach is that the loaded
scripts are necessarily coupled to the loader. The loaded scripts should
be able to be run and tested without knowing about how they were loaded,
or some global "loader.complete" flag.
Loader could steal window.onload is refire it after all scripts have
loaded, however that can still fail easily, in many cases, such as:
loaded script listens for "load" using addEventListener or attachEvent,
loaded script checks document.readyState, loaded script uses
document.write.
If the loaded script is only allowed to check a flag, complications are
eliminated.
Its ;ate here; hope that makes good enough sense!