A slight problem with putting an external script at the bottom of the page is that, if the page is large (example), and if the script isn't cached, the script will take an unnecessary additional amount of time to load:
- The page will have to be fully downloaded, then
- A network request to download the script is made, then
- The script gets downloaded and parsed
On small pages, this isn't much of an issue, since the amount of content in the HTML to download is small, and modern browsers are smart enough to look ahead to see what they'll need to download in advance, while still rendering - regardless of the placement of the script tag, a request to download it (if needed) will be made almost as soon as the page content gets returned to the browser.
Another issue with putting script tags at the bottom with large pages is that, if the page is intended to be interactable when fully loaded, the page may appear unresponsive to the user until that point. In this sort of situation, a possible approach would be to load jQuery with the async
attribute, at the top of the page, so that as soon as it's downloaded, it can be run (and attach a load
listener to the jQuery tag to run your own script after it).
On small pages, it doesn't really matter - you can achieve the desired results pretty easily using many methods. If you don't want to wrap your code in document.ready
, you can also put your script at the top and use the defer
attribute to tell the browser to run the script as soon as the DOM is finished being created. This is essentially the same as putting the script at the end of the <body>
.