Can I use:
window.addEventListner();
in some way.
All my images have a display = 'none'
.
Once the image has loaded,
I want to set display = 'inline'
This way I can normalize what is displayed while the image is being downloaded.
In this case, I can not pre-load my images.
Without libraries:
The "load" event will not trigger if the image is incidentally loaded already; thus, we check whether
complete
is already set.The
load
/onload
event does not bubble (reference, reference), so what you're asking for is not possible. You'll have to attach an event handler to each image node.You can use the
Image.onload
event handler but there's no bubbling involved.Use capturing event listener on some DOM node other than
window
(body
or other parent of image elements of interest):With this you don't have to (re)attach event handlers while iterating through
document.images
.And this will work for dynamically inserted images as well.
Same is true for image's
error
loading events. MDN: addEventListener