[whatwg] DOMContentLoaded, load and current document readiness

On Tue, May 31, 2011 at 5:02 AM, Henri Sivonen <hsivonen at iki.fi> wrote:
> Currently, there seems to be no correct non-racy way to write code that
> probes a document to determine if DOMContentLoaded or load has fired and
> runs code immediately if the event of interest has fired or adds a
> listener to wait for the event if the event hasn't fired.

How about:

var didStuff = false;
function doStuff() {
    if (didStuff) {
        return;
    }
    didStuff = true;
    // . . .
}
document.addEventListener("DOMContentLoaded", doStuff, false);
if (document.readyState != "loading") {
    doStuff();
}

> Are there compat or other reasons why we couldn't or shouldn't make it
> so that the same task that fires DOMContentLoaded changes the readyState
> to "interactive" and the same task that fires load changes readyState to
> "complete"?

. . . but obviously, this is a much better idea, since authors are not
going to figure out the code I just wrote, even if it is correct.

Received on Tuesday, 31 May 2011 15:37:09 UTC