Force re-firing of all (scripts / functions) on ajaxed content

tl;dr

I use ajax to fetch new content. The content is fetched and added to the page. However, scripts don't "re-fire" because their calls are outside of the ajaxed div.

The scripts load and fire without any problem on initial page load but not when I add new content via ajax. I get no console errors and there are no issues if I visit the URL directly.


Related:
  1. Forcing Script To Run In AJAX Loaded Page - Relates to one specific script. I want to fix (refire) all scripts including dynamic ones from Cloudflare apps
  2. Using jQuery script with Ajax in Wordpress - Same as above, this relates only to one specific script
  3. ajax loaded content, script is not executing

Intro:

I use Ajaxify WordPress Site(AWS) on a WordPress website.

The plugin lets you select a div by its id and then fetches new content inside that div using ajax

html markup

<html>

<head></head> 

<body>
  <div id="page"> 
    <header></header>
    <main id="ajax"> <!-- This is what I want to ajaxify -->
      <div class="container">
        main page content
      </div> 
    </main> <!-- end of ajaxfied content  -->
    <footer></footer>
  </div> <!-- #page -->
</body>

</html>

Problem

The plugin works and I get fresh content loaded and styled but there is an issue. Some of my page scripts and function calls are outside of the div that I use ajax on. I have two examples

1- Masonry is loaded and called in the <footer>

<html>

<head></head> 

<body>
  <div id="page"> 
    <header></header>
    <main id="ajax"> <!-- This is what I want to ajaxify -->
      <div class="container">
        main page content
      </div>  
    </main>   <!-- end of ajaxfied content  -->
    <footer></footer> <!-- Masonry script is loaded and called here -->
  </div> <!-- #page -->
</body>

</html>

2- Google maps call is in the <head>

<html>

<head></head>  <!-- Google maps is called here -->

<body>
  <div id="page"> 
    <header></header>
    <main id="ajax"> <!-- This is what I want to ajaxify -->
      <div class="container">
        main page content
      </div>  
    </main>  <!-- end of ajaxfied content  -->
    <footer></footer> 
  </div> <!-- #page -->
</body>

</html>

These are just two examples. There are others in other locations. As you can tell, such scripts won't be re-called as the only thing that reloads on the page is <main id="ajax">. While the new content inside <main> is there, some of the scripts required to render it properly are not re-called and so I end up with missing elements / broken layout.


I am not the only one who has faced this problem; a quick look at the plugin's support forum on wordpress.org shows that this issue is common.

Note: I wouldn't try to fix this if the plugin had many other issues. It works for me I just need the scripts to re-fire.

The official response is that it's possible to reload / re-fire scripts by adding the following into the plugin's php files:

$.getScript(rootUrl + 'PATH TO SCRIPT');

Which works. It works well. for example if I add

$.getScript(rootUrl + '/Assets/masonry.js');

Then the masonry function calls get re-fired when the ajaxed content is fetched even if masonry.js is loaded outside of the ajaxed div

I refer you to the plugin's files on github for more clarity on what the fix actually does (I can't make sense of what happens when $.getScript is used)


In summary

The official fix works fine if you have 3-4 scripts that need to be re-fired on ajaxed content.

This does not work for my goal because

  1. it's too rigid and hard-coded
  2. Some of the scripts are added to the page dynamically via Cloudflare apps

A possible solution might involve adding an event mimics the trigger that causes the scripts to fire at the bottom of the ajaxed div

Question:

How do I force all scripts - including dynamically added ones - to re-fire when only a certain part of the page has been reloaded via ajax?


Notes:

  1. I am trying to avoid calling out scripts one by one as that would require knowledge of their calls before hand. I am probably talking way over my head but...

    I am trying to mimic the page load and / or document ready events - at which most conditional scripts are fired (correct me if I'm wrong) - at the end of <main> in my html when new ajaxed content is added but without affecting the document when the page is loaded via using the url directly...or any other similar approach.

  2. Just for a bit of context, here is a list of some the event listeners on the page while the plugin is off. I know there are things in there I won't have to trigger. I just added this for reference. Also, please note that this is a sample taken from one of the pages. other pages may differ.

DOMContentLoaded
beforeunload
blur
click
focus
focusin
focusout
hashchange
keydown
keyup
load
message
mousedown
mousemove
mouseout
mouseover
mouseup
mousewheel
orientationchange
readystatechange
resize
scroll
submit
touchscreen
unload

Answers


The solution you choose here will have to depend on how the scripts are initialized. There are a couple common possibilities:

  1. The script's actions are evoked immediately upon loading of the script. In this case, the script might look something like this:

    (function() {
         console.log('Running script...');
    })();
    
  2. The script's actions are evoked in response to some event (such as document ready (JQuery) or window onload (JavaScript) events). In this case, the script might look something like this:

    $(window).on('load', function() {
        console.log('Running script...');
    });
    

Some options for these two possibilities are considered below.

For scripts that run immediately on loading

One option would be to just remove the <script> elements you want to trigger from the DOM and then reattach them. With JQuery, you could do

$('script').remove().appendTo('html');

Of course, if the above snippet is itself in a <script> tag, then you will create an infinite loop of constantly detaching and re-attaching all the scripts. In addition, there may be some scripts you don't want to re-run. In this case, you can add classes to the scripts to select them either positively or negatively. For instance,

// Positively select scripts with class 'reload-on-ajax'
$('script.reload-on-ajax').remove().appendTo('html');

// Negatively select scripts with class 'no-reload'
$('script').not('no-reload').remove().appendTo('html')

In your case, you would place one of the above snippets in the event handler for AJAX completion. The following example uses a button-click in lieu of an AJAX completion event, but the concept is the same (note that this example doesn't work well within the StackOverflow sandbox, so try loading it as a separate page for the best result):

<html>
  <head></head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  <script class="reload-on-ajax">
    console.log('Script after head run.');
  </script>

  <body>
    <button id="reload-scripts-button">Reload Scripts</button>
  </body>

  <script class="reload-on-ajax">
    console.log('Script after body run.');
  </script>

  <script>
     $('#reload-scripts-button').click( () => $('script.reload-on-ajax').remove().appendTo('html') );
  </script>
</html>

If you can identify a global initialising function or code block in your external scripts, you could take a look at the 'ajaxComplete' event. You can put this code in your page head and put the initialising function calls or code blocks inside the ajaxComplete callback.

$(document).ajaxComplete(function(){
    module1.init();
    $('#my_id').module2_init({
        foo : 'bar',
        baz : 123
    });
});

When the scripts you are talking about don't have such easy-to-use exposed initialising functions, but initialise themselves on scriptload, I think there will be no out of the box method that works for all scripts.


Here's what you can try -

Most of the scripts like masonry or Google Map are set to re-init on window resize. So, if you trigger the resize event after ajax complete, it will help to re-fire those scripts automatically.

Try the following code -

$( document ).ajaxComplete( function() {
    $( window ).trigger('resize');
} );

This will force the scripts to re-init once ajax is completed as it will now trigger the resize event after the content is loaded.


Maybe risky, but you should be able to use DOMSubtreeModified on your <main> element for this.

$('#ajaxed').bind('DOMSubtreeModified', function(){
  //your reload code
});

Then you should be able to just append all your scripts again in your reload area

var scripts = document.getElementsByTagName('script');
for (var i=0;i<scripts.length;i++){
   var src = scripts[i].src;
   var sTag = document.createElement('script');
   sTag.type = 'text/javascript';
   sTag.src = src;
   $('head').append(sTag);

}

Another option could be create your own event listener and have the same reload code in it.

$(document).on('ajaxContentLoaded', function(){//same reload code});

Then you could trigger an event in the plugin once the content had been updated.

$(document).trigger('ajaxContentLoaded');

Or possibly a combination of editing the plugin to trigger a listener and adding to your codebase to re-run anything you feel you need to have re-ran off that listener, rather than reload anything.

$(document).on('ajaxContentLoaded', function(){
   //Javascript object re-initialization
   myObj.init();
   //Just a portion of my Javascript?
   myObj.somePortion();
});

A solution could be duplicating all the scripts...

$(document).ajaxSuccess((event, xhr, settings) => {
  // check if the request is your reload content
  if(settings.url !== "myReloadedContentCall") {
    return;
  }

  return window
    .setTimeout(rejs, 100)
  ;
});

function rejs() {
  const scripts = $('script[src]');
  // or, maybe alls but not child of #ajax
  // const scripts = $('*:not(#ajax) script[src]');

  Array
    .prototype
    .forEach
    .call(scripts, (script, index) => {
      const $script = $(script);

      const s = $('<script />', {
        src: $script.attr('src')
      });  
      // const s = $script.clone(); // should also work

      return s
        .insertAfter($script)
        .promise()
        .then(() => $script.remove()) // finally remove it
      ;
    })
  ;

}

I had this exact problem when attempting to use ajax to reload a page with browser states and history.js, in wordpress. I enqueued history.js directly, instead of using a plugin to do that for me.

I had tons of JS that needed to be "re-ran" whenever a new page was clicked. To do this, I created a global function in my main javascript file called global_scripts();

Firstly, make sure this JS file is enqueued after everything else, in your footer.php.

That could look something like this:

wp_enqueue_script('ajax_js', 'url/to/file.js', 'google-maps', 1, true);

My javascript that I enqueue is below.

jQuery(document).ready(function($) {

    // scripts that need to be called on every page load
    window.global_scripts = function(reload) {


       // Below are samples of the javascript I ran that I needed to re run.
       // This includes lazy image loading, paragraph truncation.

       /* I would put your masonry and google maps code in here. */

        bLazy = new Blazy({
            selector: '.featured-image:not(.no-blazy), .blazy', // all images
            offset: 100
        });


        /* truncate meeeee */
        $('.post-wrapper .post-info .dotdotdot').dotdotdot({
            watch: 'window'
        });

    }

    // call the method on initial page load (optional)
    //global_scripts();

});

I then hooked into the JS that ran whenever a page was loaded with history.js and called global_scripts();

It seems as though the plugin you are using also uses history.js. I haven't tested it with your plugin specifically, but you can try what I posted below (which is what I use in my app). This would go in the same JS file above.

History.Adapter.bind(window, 'statechange', function() { 
     global_scripts();
}

My code is a bit more complicated than what is simply pasted above. I actually check the event to determine what content is being loaded, and load specific JS functions based on that. It allows for more control over what gets reloaded.

note: I use window.global_scripts when declaring the function because I have separate JS files that hold this code. You could chose to remove this if they are all in the same.

note2: I realize this answer requires knowing exactly what needs to be reloaded, so it ignores your last note, which asks that this doesn't require such knowledge. It may still help you find your answer though.


Short answer: It is not possible to this in a generic way. How should your script know which events needs to be fired?

Longer answer: It is more like a structural question than a programmatic one. Who is responsible for the desired functionality? Lets take masonry as an example:

Masonry.js itself does not listen to any events. You need to create a masonry instance by your own (which is most probably done on domready in your Wordpress plugin). If you enable the resize option it will add a listener to the resize event. But what you actually want is some listener on "DOM content change" (see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver for possible solution). Since masonry.js does not provide such a function you have the following options:

  1. Wait for the implementation (or do it yourself) in masonry.js
  2. Wait for the implementation (or do it yourself) in masonry Wordpress plugin.
  3. Add the functionality somewhere else (your template, your ajax plugin, etc.)
  4. 4.

As you can see every option includes some work to be done and this work needs to be done for every script you want to listen to your AJAX invoked DOM changes.


Need Your Help

Celery: auto discovery does not find tasks module in app

python django celery django-celery

I have the following setup with a fresh installed celery and django 1.4:

Invertible STFT and ISTFT in Python

python scipy fft signal-processing

Is there any general-purpose form of short-time Fourier transform with corresponding inverse transform built into SciPy or NumPy or whatever?