1

This has not been answered here or there. These questions are poorly worded, and the answers are all along the lines of "use DOMReady".

I want to ensure that code is run only if the page has finished loading. I cannot alter the HTML file itself, but if I could, I would do something that amounts to this:

<!DOCTYPE html>
<html>
 <head>
 <script>window.loaded=false;</script>
 </head>
 <body onload="window.loaded=true;">
 <!-- ... -->
 </body>
</html>

Then in my code:

if (window.loaded) { run(); }
else { document.addEventListener("load", run); }

And that would achieve exactly what I want. Unfortunately, I cannot modify the HTML, which means that I am looking for a way to determine whether the document has loaded (not if the DOM is ready) from JS code only.

I have looked around quite a bit, but so far, all I have found revolves around DOMReady. Has no-one really ever looked for this?

asked May 26, 2012 at 7:49
1
  • In summary DOMReady isn't anywhere in the HTML5 spec, it's a term that means different things to the different people who implemented it before the HTML5 spec was fully developed. Commented May 26, 2012 at 8:41

2 Answers 2

5

As Dr.Molle points out, document.readyState contains the property you're after:

document . readyState

Returns "loading" while the Document is loading, "interactive" once it is finished parsing but still loading sub-resources, and "complete" once it has loaded.

The readystatechange event fires on the Document object when this value changes.

The order of event firing and readyState change is defined in the end of parsing section of the HTML5 spec. In summary:

  1. The document readyState is set to "interactive" as soon as parsing is complete.
  2. The DOMContentReady event is fired almost immediately (after it's determined which resources need to be loaded).
  3. Those resources are loaded.
  4. The document readyState is set to "complete" and the load event is fired.
answered May 26, 2012 at 8:09
Sign up to request clarification or add additional context in comments.

3 Comments

I've removed some incorrect information from my post. DOMContentLoaded is not the same as readyState="complete", as described in the post-parsing section of the HTML5 spec
Indeed. So, I made this: gist.github.com/2792934. To summarise: DOMContentLoaded is runs when the DOM is loaded. onload runs after everything is loaded. readyState is set to "complete" just before that. Hence, you were both right, but I still have to wonder about all the different and ambiguous naming :)
DOMReady wasn't ever an official term, but before the HTML5 spec got to the state it's currently in people needed a term to refer to. Now I guess a lot of the confusion comes from people mixing the correct term with the old name
3

the onload-event of a window fires when the DOM and all ressources(scripts,images,stylesheets, etc.) have finished loading:

window.onload=function()
{
 //run code 
}
answered May 26, 2012 at 7:55

4 Comments

Yes, it does. But that's not what I'm asking. If my code is run after the onload event has fired, this fails.
document.readyState is another option: developer.mozilla.org/en/DOM/document.readyState . It returns "complete" when the document has been loaded.
No it's not. I have specifically asked for not DOMReady. Quoting the doc you linked to: "alternative to DOMContentLoaded".
DOM and document are two different things

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.