admin管理员组文章数量:1389762
i added code in my footer.php just before the but the code is not executed. I know it's better to use functions.php but i would like to test like this for the moment... Could someone help me please ? thanks
<script type="text/javascript">
window.onload = function () {
jQuery(document).ready(function(){ console.log('insider');
var alphabeticallyOrderedDivs = $('.team-member').sort(function(a, b) {
return String.prototype.localeCompare.call($(a).data('lastname').toLowerCase(),
$(b).data('lastname').toLowerCase());
});
var container = $('.ulcontainer');
container.detach().empty().append(alphabeticallyOrderedDivs);
$('body').append(container);})
}(jQuery);
</script>
i added code in my footer.php just before the but the code is not executed. I know it's better to use functions.php but i would like to test like this for the moment... Could someone help me please ? thanks
<script type="text/javascript">
window.onload = function () {
jQuery(document).ready(function(){ console.log('insider');
var alphabeticallyOrderedDivs = $('.team-member').sort(function(a, b) {
return String.prototype.localeCompare.call($(a).data('lastname').toLowerCase(),
$(b).data('lastname').toLowerCase());
});
var container = $('.ulcontainer');
container.detach().empty().append(alphabeticallyOrderedDivs);
$('body').append(container);})
}(jQuery);
</script>
Share
Improve this question
edited Oct 26, 2017 at 14:55
Johansson
15.4k11 gold badges43 silver badges79 bronze badges
asked Oct 26, 2017 at 14:42
PipooPipoo
491 silver badge5 bronze badges
3
- I think theme you are using enqueue all js in wp_footer hook. Try to add your js After wp_footer and check it. – Darshan Saroya Commented Oct 26, 2017 at 15:34
- sorry but i put direly in footer.php before </body> and jQuery(document).ready(function() is not executed – Pipoo Commented Oct 26, 2017 at 16:26
- here it works without adding JS in settings but when adding jQuery it does not work anymore ...jsfiddle/xo5nopyx – Pipoo Commented Oct 26, 2017 at 16:40
1 Answer
Reset to default 5By Using wp_enqueue_script()
You can add your scripts to a JS file and then enqueue it properly. I assume you have added your scripts to file.js
. Here's how you enqueue it in the footer:
add_action('wp_enqueue_scripts','my_script_callback');
function my_script_callback(){
wp_enqueue_script(
'my-script',
get_template_directory_uri().'/folder/file.js',
array(jquery),
null,
true
);
}
The last argument in wp_enqueue_script()
determines whether your script should be enqueued in the footer or not.
The 3rd argument is optional, and is an array of dependencies.
By Using wp_footer()
You can also directly print your scripts in the footer, which is not always the best idea. However, this is how you do it:
function my_script() { ?>
<script type="text/javascript">
// Your code here
</script><?php
}
add_action( 'wp_footer', 'my_script' );
Not that you have to wait for jQuery to be ready in such a method. You can enqueue jQuery if not already enqueue, by using this:
add_action( 'wp_head' , 'enqueue_jquery' )
function enqueue_jquery() {
wp_enqueue_script( 'jquery' );
}
本文标签: javascriptHow to add JS in footer
版权声明:本文标题:javascript - How to add JS in footer 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744723854a2621856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论