0

In what situations in javascript do we need to wait for the window onload event before running code?

And what useful things can be done without needing to wait?

J_men
  • 347
  • 1
  • 4
  • 13
  • 1
    Do you mean as opposed to `DOMReady`, or in general? – Frédéric Hamidi Sep 01 '14 at 12:36
  • If your script is BEFORE html then you must wait it loads (otherwise what you'll try to access something that doesn't exist). If it's AFTER markup then you _may need_ to wait only for asynchronous context (such as images) because they'll partecipate on layout. Finally DOM is ready and available only...when it has been parsed (but when it's ready you may use it before onload). – Adriano Repetti Sep 01 '14 at 12:38
  • @FrédéricHamidi - I mean in general – J_men Sep 01 '14 at 12:39

4 Answers4

3

In what situations in javascript do we need to wait for the window onload event before running code?

  • If you want to access nodes in the #document
  • If you want to get values such as the height or width of the page
  • If you want to avoid race conditions when appending new things to the #document
  • Basically, everything DOM related to the specific page

And what useful things can be done without needing to wait?

  • Computational actions
  • Declaring functions, initialising variables
  • Testing feature support, including polyfills
  • Basically, everything non-DOM related, or DOM related to the specific browser but not the page
Paul S.
  • 58,277
  • 8
  • 106
  • 120
1

From a related SO post: window.onload vs. body.onload vs. document.onready

window.onload waits until all assets have finished downloading, such as images and scripts.

There is also a "DOM ready" event that allows quicker access to the dom, as it only waits until you can access the DOM via the API.

Community
  • 1
  • 1
ne1410s
  • 5,806
  • 6
  • 48
  • 58
1

You need to use window onload event before running code when your code needs to access/modify DOM and all the assets (images, fonts, etc). You might also use jQuery's (or equivalent) &(document).ready(). It fires earlier than window onload, so users will feel like website loads faster. The difference is that on document ready only DOM tree is ready, but assets might not be loaded yet. On window onload, everything's already loaded.

Before document ready event, you might still do some stuff. For example, if you need to create some kind of a unique id for a user and set a cookie, there's no need to wait for DOM. Or if you want to do something based on cookies (provided it doesn't touch DOM). Or you need to load some external data from an API (but you need to wait for DOM before trying to display that data). In general, anything that doesn't touch DOM should be ok.

0

If your code need other files to be executed (like libraries, frameworks or your own file that expose global things), you should always wait that the browser had finished to download everything.

So in most case, you should do this. You could find edges cases that don't need this but it's very rare and it doesn't cost you very much in a human perception of time.

gaelgillard
  • 2,283
  • 12
  • 20