For Developers

How to Execute a JavaScript Function After Uncached Content Loads

If you need to run a custom JavaScript function immediately after uncached content has been loaded, you can use the "content_no_cache_added" event.

In the example below, we use this event to dynamically load the script from script-url/your-script.js:

add_action( ‘wp_footer’, function() {
  ?>
  <script>
  document.addEventListener(‘content_no_cache_added’, function () {
    console.log(‘trying to enqueue a script’);
    let script = document.createElement(‘script’);
    script.src = ‘script-url/your-script.js’;
    script.async = true;
    document.body.appendChild(script);
  });
  </script>
<?php
} );