Blog Post

Lately Loading JavaScript

You want to load your JavaScript code as late as possible in the downloading of the site—that is just before the closing body tag. If you load it earlier, such as in the head element, it can block the download of other assets (like zombies blocking a doorway with their bodies after you smack them with an HTML element). The total time spent downloading will be the same wherever the JavaScript is placed, but if it blocks download or rendering because the browser is waiting on your js file to download or your js code to execute, it can prevent the rest of the page from displaying, giving the user a blank screen for longer. Even if the page isn’t functional until the js loads, the user likes to see that things are working, and the browser rendering what it can will help. A blank page, like a zombie’s blank stare, usually means something is broken and users will assume as such. Some JavaScript may need to be executed before the page loads or to determine how the page loads, so the head may have some JavaScript in it, but push it to the bottom if you can.