jQuery in header or footer

From my reading it seems there is a good case for loading scripts like jquery and dependencies into the footer as a preference if possible; however 2 things confuse me which. Firstly the default for wp_enqueue_script() is to put the script in the footer and second (and possibly related) there is a line from the codex

Note that you have to enqueue your script before wp_head is run, even if it will be placed in the footer.

Is this to say that even if the script is set to load into the footer it still loads early on and so we loose several of the advantages of footer placement?

Edit – Just in case the above incorrect statement confuses anyone in future. I realised since that I’d been reading the codex wrong and that the default for wp_enqueue_script() is NOT to put the script in the footer

2 Answers
2

There is a lot of leftovers in script-related articles in Codex that are not entirely correct (putting it mildly).

The enqueue should not be done before wp_head(), it should be done on wp_enqueue_scripts. Which is technically early inside wp_head().

It doesn’t harm performance, because registering/enqueueing script is merely explaining to WordPress how it should be done. Actual script output is done as separate print action.

See my answer in where is the right place to register/enqueue scripts & styles for more detailed description of how things work.

Leave a Comment