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


#1

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?


#2

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


#3

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)?


#4

Use DOMContentLoaded event of the document object.