admin管理员组文章数量:1434370
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' );
本文标签: theme developmentHow to properly add and access a JavaScript file in Wordpress
版权声明:本文标题:theme development - How to properly add and access a JavaScript file in Wordpress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745592691a2665291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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