简体   繁体   中英

JS file not loading on Wordpress site

Apologies, I have literally no idea when it comes to .js stuff; I'm a designer mostly and so far have never needed to use any js.

I'm soon to build a site for a client in Wordpress, and they have a slide-based calculations function that I need to lift from their old site; only trouble being that I will be constructing the site using a different theme, so the functionality needs re-integrating into the new theme.

Trouble is, in testing this I can get the slides to appear, but they don't really do anything - and it seems that the problem is that the associated .js file isn't loading. It's been placed in THEME/assets/js/ where it seems it should be, and I have added the following to functions.php:

wp_enqueue_script( 'rangeslider-js', get_template_directory_uri() . '/assets/js/rangeslider.js', array( 'theme-js' ), '1.0', false);

Can anyone point me in the direction of what I'm doing wrong please? Sorry for my complete amateurness - I hope the above makes sense...!

It may be that maybe jquery is not loaded or that there is a conflict with something else.

try

function myFunction(){
wp_enqueue_script( 'rangeslider-js', get_template_directory_uri() . '/assets/js/rangeslider.js', array( 'jquery','theme-js' ), '1.0', false);
}
add_action( 'wp_enqueue_scripts', 'myFunction' );

So first check the source code on the page and make sure it is indeed not being sourced in. If it is being sourced in, make sure that the path actually links to the source code file. If its just not there consider your statement:

wp_enqueue_script( 'rangeslider-js', get_template_directory_uri() . '/assets/js/rangeslider.js', array( 'theme-js' ), '1.0', false);

Try replacing array( 'theme-js' ) with NULL - just to see if it that allows it to show up.

wp_enqueue_script( 'rangeslider-js', get_template_directory_uri() . '/assets/js/rangeslider.js', NULL, '1.0', false);

That parameter requires the javascript file with the handle 'theme-js' to be loaded first or your script wont even try to load. Replacing it with NULL will remove the perquisites required and wordpress will load your script if it exists.

If that allows your script to load, check and make sure the prerequisite script is loaded and see if it using the handle 'theme-js'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM