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...

Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102

2 Answers2

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.

Code Spirit
  • 3,992
  • 4
  • 23
  • 34
  • This isn't true anyway because when I add '/' at the beginning it will go to the root directory – Tom el Safadi Sep 24 '16 at 23:26
  • No its not, its actually not at the beginning of the path. Have you checked your browser console? What path is it outputting? – Code Spirit Sep 24 '16 at 23:29
  • http://stackoverflow.com/questions/5559578/having-links-relative-to-root why is this saying that '/' will go to the root? – Tom el Safadi Sep 24 '16 at 23:32
  • 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](https://developer.wordpress.org/reference/functions/wp_enqueue_script/). – Code Spirit Sep 24 '16 at 23:33
  • Yeah, Google is always right. I would recommend to you to read the whole answer and not the first lines. – Code Spirit Sep 24 '16 at 23:36
  • Okay the file is loaded now... still don't get it why. Thanks – Tom el Safadi Sep 24 '16 at 23:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124128/discussion-between-code-spirit-and-anokrize). – Code Spirit Sep 24 '16 at 23:43
  • Somehow it doesn't hide the elements which I did with jQuery. Any idea why? jQuer is loaded successfully though. – Tom el Safadi Sep 24 '16 at 23:43
  • http://stackoverflow.com/questions/39682085/wordpress-external-script-not-working see this please. – Tom el Safadi Sep 24 '16 at 23:57
0

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>
unixmiah
  • 3,081
  • 1
  • 12
  • 26