0

So we have the webpage, and below that, a script with window.onload / document.onload listener.

When the browser loads the page, imagine that the webpage is loaded before reach the line code of the listener. It will fire anyway?

Angel Luis
  • 487
  • 2
  • 5
  • 19

1 Answers1

0

If you add a load event handler after the load event has fired, then the event handler will not be called.

It's the same as any other event.

addEventListener("load", () => {
  console.log("Load event");
  addEventListener("load", () => {
    console.log("Second load event");
  });
});
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I understand that the only way to get the listener working in all cases is put the listener in the header. It's correct? – Angel Luis Jan 14 '19 at 20:05
  • @AngelLuis — No. A load event can't fire before the document has loaded, and that isn't going to happen before it has reached the end of the document. – Quentin Jan 14 '19 at 20:10
  • sorry then I don't understand your explanation. If I have a script before the

    , and inside the load listener, it's not supposed to fire after load all the script?

    – Angel Luis Jan 14 '19 at 20:17
  • You can't have a "script" inside a load listener, unless you mean that you have some code to dynamically add a script element to the DOM inside the load listener, in which case it doesn't matter where the script is because you still won't be adding the script before the load event fires. – Quentin Jan 14 '19 at 20:18