When creating event handlers in a Rails/Turbolinks-enabled app, is it best to delegate to document.body
or document
? Is there an accepted convention?
I've read that delegating to document
is slightly less performant. However, it does allow you to avoid registering ready
callbacks throughout your javascript codebase. Assume my scripts are in the <head> as is recommended for Turbolinks.
An example using jQuery (although the question isn't really about using jQuery):
$(document).on('click', 'selector', callback);
// versus
$(document).ready(function() {
$(document.body).on('click', 'selector', callback);
});