A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Why does document.addEventListener(‘load’, handler) not work?

trusktr
2015-11-11

I’m trying

document.addEventListener('load', handler)

in Chrome 46 and Firefox 42, and it’s not working. I can get it to work if I try it on window, as in

window.addEventListener('load', handler)

The MDN documentation for the “load” event says

Target: Document, Element

So why doesn’t it work on the document? Do the docs need to be updated?

simevidas
2015-11-14

The spec states that it is fired at window and at certain (special) elements like <img>:

Fired at the Window when the document has finished loading; fired at an element containing a resource (e.g. img, embed) when its resource has finished loading

Source: https://html.spec.whatwg.org/multipage/indices.html#events-2

trusktr
2015-12-05

Is window.addEventListener('load', handler) the proper way to do things when the document is ready (DOM loaded, but images, etc still need to load)?

MT
2015-12-05

Use DOMContentLoaded event of the document object.