As an issue of strictness, even though doing otherwise is common, <script> is only allowed in <head> by the HMTL DTD.
The reason we used to do this in the bad old days (and that Google Analytics still recommends this practice to this day) is that it would mean that the script was the last thing that got loaded, so it wouldn't slow down the loading of the rest of the site.
That's not really how things work anymore. That's from before the browser would even open multiple connections to get assets, from before there was defer, from before there was window.onload, et cetera.
Instead of putting the <script> at the end of <body>, please put it in the <head> and mark it defer="defer". This will mean that whereas it'll be downloaded immediately, it won't actually be executed until everything's downloaded, and then only in the order that they're nominated.
As an issue of strictness, even though doing otherwise is common,
<script>
is only allowed in<head>
by the HMTL DTD.The reason we used to do this in the bad old days (and that Google Analytics still recommends this practice to this day) is that it would mean that the script was the last thing that got loaded, so it wouldn't slow down the loading of the rest of the site.
That's not really how things work anymore. That's from before the browser would even open multiple connections to get assets, from before there was
defer
, from before there waswindow.onload
, et cetera.Instead of putting the
<script>
at the end of<body>
, please put it in the<head>
and mark itdefer="defer"
. This will mean that whereas it'll be downloaded immediately, it won't actually be executed until everything's downloaded, and then only in the order that they're nominated.