Archived topic
Can JQuery Move to Footer?
8 replies · Started by William on April 30, 2021
Hello,
I'm trying to optimize my site and one of the largest render blocking resources seems to be JQuery. If I move this to the footer, will it mess anything up on the theme?
Hi William,
I don't believe that's a good idea.
Looks like you are using the sticky navigation which is the only remaining option that uses jQuery.
We are working on a solution to replace it with pure CS.
As for now, I'd say that if you can live without it then that's the way to go.
Let me know if this helps :)
Thanks. Are there other options to not use a sticky navigation?
Sorry I'm not sure what you mean?
We can already make the header fixed/stick with the CSS below. Just not possible with the hide when scrolling down option.
.main-navigation {
position: sticky;
top: 0;
}
May I drop in with a solution?
/*Move jQuery from Header to Footer*/
add_action( 'wp_enqueue_scripts', 'jquery_in_footer' );
function jquery_in_footer() {
wp_deregister_script( 'jquery' );
wp_register_script(
'jquery',
includes_url( '/js/jquery/jquery.js' ),
false, // no dependencies
NULL, // no version to show
true // in footer? yes
);
wp_enqueue_script( 'jquery' );}
Thanks Alex!
Really glad to help!
Sorry, I'm not super familiar with coding. When I said can I make it 'not sticky' I assumed that would remove the JQuery completely.
Alex, which file do I add that to and does it need to be in any specific place?
Thanks!
When I said can I make it ‘not sticky’ I assumed that would remove the JQuery completely.
Yes that's correct.
Alex, which file do I add that to and does it need to be in any specific place?
That code can be added with one of these methods:
Adding PHP: https://docs.generatepress.com/article/adding-php/