miahelf
miahelf

Reputation: 834

How wait to run JavaScript function until after the DOM is ready / loaded?

I'm using a JavaScript upload script that says to run the initialize function as soon as the DOM is ready. I currently have it working just fine with either a call to the function with body.onload or directly after the function is defined. The function builds some HTML in a placeholder div that acts as the file uploader tool.

My question is what is the best practice here? Since it works for now, why would the instructions say to run the init function as soon as the DOM is ready? Should I be adding a <script> tag directly after the placeholder DIV for example?

Upvotes: 40

Views: 68595

Answers (7)

Robert Dyas
Robert Dyas

Reputation: 2068

In 2015 you have two options with modern browsers:

document.onload
  • this fires when the document is loaded, but other resources (most notably images) have not necessarily finished loading.
window.onload
  • this fires when the document is loaded, AND all other resources (again, most notably images) are loaded.

Both of the above events would be better utilized with window.addEventListener() of course, as multiple listeners would be allowed.

Upvotes: 7

agiopnl
agiopnl

Reputation: 1354

You could also just move the <script> to the bottom of your page like this:

<html>
<body>
    <main></main>
    <script>
        // code
    </script>
</body>
</html>

Upvotes: 3

user1012851
user1012851

Reputation:

Get jQuery and use the following code.

$(document).ready(function(){
    // Do stuff
});

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318698

The easiest solution is using jQuery and its $(document).ready(function() { .... }); function. Instead of .... you put your own code.

Note that it basically does the same thing @Shadow2531 suggested, but also works in old browsers not supporting that event.

Upvotes: 6

Alex
Alex

Reputation: 2011

As you probably know you should not run init functions before the DOM is fully loaded.

The reason you must run the init function as soon as the DOM is ready, is that once the page has loaded the user starts hitting buttons etc. You have to minimize the small inavoidable gap where the page is loaded and the init-functions haven't run yet. If this gap gets too big (ie. too long time) your user might experience inappropiate behaviour... (ie. your upload will not work).

Other users have provided fine examples of how to call the init function, so I will not repeat it here... ;)

Upvotes: 2

Ariel
Ariel

Reputation: 26783

The DOM is usually ready before onLoad runs. onLoad only runs after everything loads - external scripts, images, stylesheets, etc.

But the DOM, i.e. the HTML structure is ready before that. If you run the code at the bottom of the page (or after the parts of the page the script works with) that will work fine as well.

Upvotes: 7

Shadow2531
Shadow2531

Reputation: 12170

<script>
    window.addEventListener("DOMContentLoaded", function() {
        // do stuff
    }, false);
</script>

You do that so you know all the parsed elements are available in the DOM etc.

Upvotes: 72

Related Questions