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:
- Code inside onload() event handler
- 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!
2
Answers
window.onload
waits for all external resources and synchronous Javascript to load, but it doesn’t wait for:<script async src="...
.<script defer src="...
.<script type="module" src="...
.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
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