skip to Main Content

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

Answers


  1. 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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search