How to enqueue a script depending on another script ?
-
Hi,
I created a librarie that I’m trying to load before loading a specific page script which will use functions of this librarie :
I tried some ways.
First I tried to pass the second script in footer and add the handle of the first script in its dependencies :add_action( 'wp_enqueue_scripts', 'add_theme_scripts' ); function add_theme_scripts() { wp_enqueue_script( 'from-side', get_template_directory_uri() . '/libs/gway/from-side.js', array( 'jquery' ), null ); if ( is_page_template( 'page-projects.php' ) ) { wp_enqueue_script( 'gw_projects', get_template_directory_uri() . '/libs/gway/gw_projects.js', array( 'jquery', 'from-side' ), null, true ); } }
But I got the error : Uncaught ReferenceError: fs_init is not defined
So here, the gw_projects.js script is still loaded before from-side.js (where the function fs_init is declared).I tried to call two add_action() functions with priorities :
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' ); function add_theme_scripts() { wp_enqueue_script( 'from-side', get_template_directory_uri() . '/libs/gway/from-side.js', array( 'jquery' ), null ); } add_action( 'wp_enqueue_scripts', 'add_theme_script_after', 100); function add_theme_script_after(){ if ( is_page_template( 'page-projects.php' ) ) { wp_enqueue_script( 'gw_projects', get_template_directory_uri() . '/libs/gway/gw_projects.js', array( 'jquery', 'from-side' ), null, true ); } }
But I still get the same error.
Is there a solution to force the ‘from-side.js’ script to be loaded before ‘gw_projects.js” ?Thank you in advance,
N.
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘How to enqueue a script depending on another script ?’ is closed to new replies.