1

I have read many times that one should place javascripts at the bottom of the body tag to help page load. But while going through the source code of https://shoprex.com/ I saw that they had placed alot of javascripts (both inline and external) AFTER the closing body tag and BEFORE the closing html tag.

</body>
<script>
//some code
//some code
//some code
//some code
//some code
</script>
<script src="example.js">
</script>
</html>

Does this make a difference? Is there any advantage/disadvantage in this or is it just uncommon? Do all browsers respond to this identically or are there compatibality issues?

Asif Ali
  • 271
  • 1
  • 3
  • 16
  • 3
    Possible duplicate of [Is it wrong to place the – Dijkgraaf Jun 17 '17 at 00:30

1 Answers1

3

Javascript runs when it's encountered in the markup. If you are manipulating the page DOM then the nodes you want to affect must exist when the javascript runs, which is why people place the scripts after all of the page content.

Either before or after the close </body> tag should make no difference, and in my experience it doesn't, because the script is encountered and therefore run at essentially the same time, after the DOM has been built.

But see the suggested duplicate for reasons to put it inside the body.

Stephen P
  • 14,422
  • 2
  • 43
  • 67