I am writing a drag drop handler and wish to set the eventListeners
to the highest level - which I presumed would be the body
. However, I noticed in the example at MDN, they set the listeners to document
versus document.body
which leads me to ask why would this be preferable and in general, why would I choose listeners attached to one versus the other (and do they both support the same listeners)?
So when do I use document.body.addeventListener()
versus document.addEventListener()
?
UPDATE This SO question addresses WHEN to bind the events to document
versus at the element
level. Very helpful.
UPDATE2 Interestingly enough, when document.addEventListener()
is set for all the drag drop listeners, Firefox hangs with a drag (Chrome does not). When I change it to document.body.addEventListener()
for dragEnter, dragOver, dragLeave, drop
it all works fine. Seems like dragStart
wants to be on the document
however.