I've got javascript (a) and jQuery script (b). I need (b) to run after (a) has finished running. I need them to run sequentially. How do I do that?

In wp_enqueue_script, I've made (b) dependent on (a), but it appears that (b) runs before (a) has finished processing.

I'm trying to use the_maps_bounds, a variable I've put in the global scope. It gets set in (a) and used in (b). The problem is that (b) is trying to use the variable before it has been set.

Any suggestions?

Thank you.

  • 1
    You need to post the code for (a) so we can see how/when/where is the variable set Sep 29 '11 at 18:18
  • FYI: jQuery IS javascript. One and the same.
    – Chris
    Sep 29 '11 at 18:22

To accomplish this, wrap wp_enqueue_script in a function and run it through the action hook wp_enqueue_scripts while using the 'priority' parameter available in add_action to set the load order.

function these_go_first() {
    wp_enqueue_script('first_script', '[path to file]/first.js', array('jquery'), '1.0' );
function these_go_first() {
    wp_enqueue_script('after_script', '[path to file]/after.js', array('jquery', 'first_script'), '1.0' );
add_action('wp_enqueue_scripts', 'these_go_first', 1);
add_action('wp_enqueue_scripts', 'these_go_after', 2);

wp_enqueue_script can also define script dependencies as well. This is why I added 'first_script' which I had already defined in the function just before, to the dependencies array in the second function.

  • I doubt that this is the issue here. He is probably setting that variable after an ajax event or something... Sep 29 '11 at 19:14
  • hmmm... well running the scripts in the correct order using this could help. Sep 30 '11 at 0:17
  • Hi @DrewGourley Thank you for the help. It was a timing problem. My function was firing before the map fully loaded. I added your code, and the timing was still off. So in addition, I added an eventListener. It works now. Thank you.
    – Laxmidi
    Sep 30 '11 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.