Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save abha57/6f0b4743000072719656afdb70768499 to your computer and use it in GitHub Desktop.

Select an option

Save abha57/6f0b4743000072719656afdb70768499 to your computer and use it in GitHub Desktop.
Page: DOMContentLoaded, load, beforeunload, unload
DOMContentLoaded – the browser fully loaded HTML, and the DOM tree is built, but external resources like pictures <img> and stylesheets may not yet have loaded.
load – not only HTML is loaded, but also all the external resources: images, styles etc.
beforeunload/unload – the user is leaving the page.
When the browser processes an HTML-document and comes across a <script> tag, it needs to execute before continuing building the DOM.
That’s a precaution, as scripts may want to modify DOM, and even document.write into it, so DOMContentLoaded has to wait.
So DOMContentLoaded definitely happens after such scripts.
There are two exceptions from this rule:
Scripts with the async attribute, that we’ll cover a bit later, don’t block DOMContentLoaded.
Scripts that are generated dynamically with document.createElement('script') and then added to the webpage also don’t block this event.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment