using Sajax with multiple scripts

My CMS is getting pretty large. So, in order to save the end-user from downloading potentially 100ks of javascript, I’ve started modularising my code; separating the scripts based on their overall purpose.

A problem arose today, where I was using Sajax from two separate external scripts for two different parts of my document. Suddenly, I was getting Error: some_function not callable alerts.

A bit of digging revealed that the stub handler of Sajax, sajax_do_call(), has the URL of the script that created it hard-coded into itself.

Because the handler is a function, it meant that when script A and script B were loaded, the stub handler of script B would overwrite the handler for script A, so the scripts would basically forget how to call the PHP side of the transactions.

So, there was a dilemma – fix Sajax, or only use one Ajax script per page. The answer to that was simple – fix Sajax.

The solution is to rip out the hardcoded URL in sajax_do_call():

 uri = "<?php echo $sajax_remote_uri; ?>";

and replace it with a more extendable version:

 uri = function_urls[func_name];

So now, when Sajax is asked to handle a function, instead of requesting the server response from a hard-coded source, it checks frist to see what source the function was defined by.

To fill the function_url with its required values, place the following line after the definition of x_<?php echo $func_name; ?>() (it’s in sajax_get_one_stub()):


The next bit was tricky – we need to initialise the array function_urls, but only if the variable has not already been initialised (otherwise, script B would again overwrite script A).

I couldn’t find a JavaScript equivalent of isset(), so had to come up with an ugly piece of code which does the job (which I described in an earlier post).


2 Replies to “using Sajax with multiple scripts”

Comments are closed.