How can I have my JavaScript run every time AJAX finishes adding content to the page?
My JavaScript file loads and runs, but only on first page load. I'm looking for the equivalent of Drupal.behaviors.attach. Is there such a thing on WordPress?
How can I have my JavaScript run every time AJAX finishes adding content to the page?
My JavaScript file loads and runs, but only on first page load. I'm looking for the equivalent of Drupal.behaviors.attach. Is there such a thing on WordPress?
Share Improve this question asked Dec 23, 2019 at 18:12 htoiphtoip 1012 bronze badges 2- What's the mechanism that's making AJAX requests and adding content to the page? I don't think there's one included in WordPress or the default themes. So you're using a mechanism from a plugin? You'll need to find out how to hook into that. – Rup Commented Dec 23, 2019 at 23:49
- In this case it was a plugin that used $.ajax. When finished it fired the "widget:loaded" event, but even after I found that my event handler didn't catch it because their plugin used a custom version of jQuery different from the one my event handler was registered on. – htoip Commented Dec 24, 2019 at 13:59
1 Answer
Reset to default 0Found this at https://codex.wordpress/AJAX_in_Plugins
JavaScript triggering the
post-load
event after content has been inserted via Ajax:
jQuery( document.body ).trigger( 'post-load' );
JavaScript listening to the post-load event:
jQuery( document.body ).on( 'post-load', function () {
// New content has been added to the page. } );
I suppose it's up to each plugin to trigger whichever event it wants when it's done or none at all.