0

I am having a wordpress website and some custom javascript. However my goal is to load this javascript file the right away as wordpress is suggesting it. Somehow this is not working or I am doing something wrong. The file is called FormScript.js and is located here:

/httpdocs/wp-content/themes/Avada/FormScript.js

In the same directory is my function.php, in which I want to load the script.

This is how I am doing it:

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {

    // Register and Enqueue a Script
    // get_stylesheet_directory_uri will look up child theme location
    wp_register_script( 'FormScript', get_stylesheet_directory_uri() . 'FormScript.js');
    wp_enqueue_script( 'FormScript' );
}

But why is this not working? Thanks for help...

2 Answers 2

2

You are missing a / after get_stylesheet_directory_uri() inside wp_register_script.
It should be:

wp_register_script( 'FormScript', get_stylesheet_directory_uri() . '/FormScript.js');

Edit:
From the documentation:

Note: Does not contain a trailing slash.

Sign up to request clarification or add additional context in comments.

9 Comments

This isn't true anyway because when I add '/' at the beginning it will go to the root directory
No its not, its actually not at the beginning of the path. Have you checked your browser console? What path is it outputting?
stackoverflow.com/questions/5559578/… why is this saying that '/' will go to the root?
Actually its not. It depends where you place your / and you should read it more precise and understand it, not just copying code. Here is the documentation.
Yeah, Google is always right. I would recommend to you to read the whole answer and not the first lines.
|
-1

why don't you open the header.php file and use a simple script tag to load the FormScript.js file?

/httpdocs/wp-content/themes/Avada/FormScript.js

/httpdocs/js/FormScript.js

http://www.domain.com/js/FormScript.js

make sure your js file is world accessible

<script type="text/javascript" src="http://www.domain.com/js/FormScript.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.