Is the Window.load event fired after all non-postponed javaScripts have finished execution?

79 Views Asked by At

Is the Window.load event fired after all "non-postponed JavaScripts" have finished execution? "Non-postponed Javascripts" are defined as any JavaScripts loaded by the HTML page (inline or external or async or dynamically-generated or module), except for:

  1. Code inside onload() event handler
  2. Code inside any other handlers that await user inputs

The spec simply said that the load event fires "when the document has finished loading". It does not say whether or not the event fires before or after all non-postponed JavaScripts have finished execution.

The following posts are related, but do not answer this question directly.

Thanks!

Related Posts:

2

There are 2 best solutions below

2
Manuvo On

window.onload waits for all external resources and synchronous Javascript to load, but it doesn't wait for:

  1. Asynchronous scripts <script async src="....
  2. Defered scripts <script defer src="....
  3. Modules <script type="module" src="....
  4. Event handler code eg.: onload(), onclick(). this not executed until the event happens.

Resources:

https://flaviocopes.com/javascript-async-defer/ https://html.spec.whatwg.org/multipage/scripting.html#the-script-element https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event

0
Emma On

WHATWG maintains the HTML Living Standard. Therefore, if they say that the "load" event is triggered "when the document has finished loading, " then that is the behaviour that browsers follow. The scripts do not necessarily finish before "onload".

https://html.spec.whatwg.org/multipage/indices.html#event-load