Patrick Gregorio Patrick Gregorio - 1 month ago 5x
HTML Question

HTML Script Location

Has the standard for including scripts changed again? Last time I checked everyone suggested that developers put their scripts just before the closing


On the Google Analytics guide they're now saying to put the script right after the opening


Your main.js file or whatever you call it, is usually better placed at the end of your body tag. You can also put your Google Analytics scripts at the end of the page, but I think you may miss some events at that point (when all the DOM will be ready).

Also, you can load scripts like Google Analytics passing the async attribute to it, ie.: <script src="googleAnalytics.js" async></script>. In this case, it's totally ok to have it at the beginning of your body tag as it won't hold the loading of the DOM.