sometimes the users bind the events on $('body')
and sometimes on $(document)
$(document).on('click', someAction);
$('body').on('click', someAction);
Is there some reason to prefer one to another?
For me, there is mainly one reason to bind the events on $(document)
and not to $('body')
:
no need to wait domReady (document is available before everything else)
Short answer most likely is, no, not really.
The reason someone is doing it should always be that he requires to catch an event globally in his markup. Since the <body>
tag should follow as direct sibling to <html>
, all events bubbling phase will end there.
<html>
<body>
<div>
</div>
Every click event on <div>
would bubble up to <body>
as well as <html>
(if not stopped manually). So for that usecase it should not make any difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With