When is window.onload fired?

window.onload (a.k.a body.onload) gets fired after the main HTML, all CSS, all images and all other resources have been loaded and rendered. So if your images stall, that can take some time.

If you just need the HTML (DOM), you can use jQuery’s $(document).ready() – it will fire when the HTML has been parsed but before the browser has finished loading all external resources (images and style sheets that come after your script element in the HTML).

Scripts embedded in the page get executed when the browser parses the </script> of each. So to execute a script before any other script, add a <script> tag in the header just after <head>.

This means you can “emulate” window.onload by adding a <script> tag as the last element of the <body>.

Leave a Comment