I intend to use a JavaScript library on my WP website and have uploaded the JS file via FTP to the following directory belonging to the currently activated theme Salient: /public_html/wp-content/themes/salient/js
Then, in order to load the JavaScript, I have added the following line in the "Header and Footer" settings (to be injected before the body closing tag):
<script type="text/javascript" src=".../public_html/wp-content/themes/salient/js/javascriptfile.js"></script>
When I am then loading a page and when inspecting it using the Chrome console, there's a 404 error for that JS file, although the file and the directory exist. As an alternative approach I also tried to add the script reference directly in HTML and even the page template, but with the same result.
Why do I keep getting the 404 error? Thanks for your help.
I intend to use a JavaScript library on my WP website and have uploaded the JS file via FTP to the following directory belonging to the currently activated theme Salient: /public_html/wp-content/themes/salient/js
Then, in order to load the JavaScript, I have added the following line in the "Header and Footer" settings (to be injected before the body closing tag):
<script type="text/javascript" src="https://www.mypage.../public_html/wp-content/themes/salient/js/javascriptfile.js"></script>
When I am then loading a page and when inspecting it using the Chrome console, there's a 404 error for that JS file, although the file and the directory exist. As an alternative approach I also tried to add the script reference directly in HTML and even the page template, but with the same result.
Why do I keep getting the 404 error? Thanks for your help.
Share Improve this question asked Apr 13, 2019 at 18:13 F.MarksF.Marks 1233 bronze badges 3 |1 Answer
Reset to default 1You should enqueue your script using
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer);
Where,
$handle
is the name for the script.
$src
defines where the script is located.
$deps
is an array that can handle any script that your new script depends on, such as jQuery
.
$ver
lets you list a version number.
$in_footer
is a boolean
parameter (true
/false
) that allows you to place your scripts in the footer of your HTML document rather then in the header, so that it does not delay the loading of the DOM tree.
Documentation
Add this code to functions.php
file of your theme.
function add_my_script() {
wp_enqueue_script( 'my-script',
get_template_directory_uri() . '/js/javascriptfile.js',
array ( 'jquery' ),
false,
true
);
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );
src
attribute and result is 404 error. It should be something like<script type="text/javascript" src="https://www.mypage/wp-content/themes/salient/js/javascriptfile.js"></script>
. – Qaisar Feroz Commented Apr 13, 2019 at 19:18