Why <script> tags should be placed at the end of body tag?

html

As we know that HTML is loaded and executed line by line. So, when the browser encounters a <script> tag, it loads and executes the javascript code on the spot. This may slow down the page rendering speed and thus webpage will take more time to load.

1
2
3
4
5
<body>
  <!-- Other contents -->
  <!-- .... -->
  <script src="myscripts.js"></script>
</body>

Javascript is often used to manipulate DOM and add new functionality to the webpage. If <script> tag is not added at end of the <body> tag, DOM may not be ready by that time, thus preventing javascript to work on it, leading to unknown behaviors.

Therefore, we should always add <script> tag at the end of the <body> tag to prevent unknown behaviors and make the webpage load faster.

Share and support us

Share on social media and help us reach more people