2 Reasons! Why Should External JS Placed Before Closing Body Tag?
We know JavaScript allow us to place JS external file link in two different location in HTML file, first is in head tag and second is in body tag (before closing tag), but some time errors, page reload delay and many other reasons are not allow us to place JS external file in head tag, that is why its alternate way to place before closing body tag is consider is good practice.
Here are advantages and reasons, why should place JS external file in before closing body tag, you can consider all advantages of this way are disadvantages of placing JS external file in head tag…
Let’s dive in right now.
1.?Improve Reload Delay Time
By placing JS file in body tag you can reduce the page reload time. How its work, let explore.
When you place JS file in head tag then browser try to fetch and execute JS code without complete rendering HTML code, that cause problem in DOM manipulation as well as reloading page content ( HTML code) which became a reason of delaying page content and increase the page load time.
If we place JS file before closing body tag then in this case browser already had render the page content, so it become easy to manipulate the DOM with help of JS that’s play an essential role in decreasing the page reload time and JS code easily executed without blocking any piece of code.
领英推荐
2.?Reduce the Errors
We also have experience this when we place JS external file in head tag it cause sometimes errors. So why these errors come, let cover it.
When JS file place in head tag then in this case still browser had not completely render page content, that is why some time during DOM manipulation JS not access the DOM elements which cause errors like null.
But in the case of placing JS file in body tag, page contend already had loaded so it’s much become easier for JS to manipulate HTML code.
Conclusion
So, the moral of the story by small step you can decrease your web page reload time and save your time in finding and fixing the errors. The step is simple just place JS external before closing body tag. So next not repeat this mistake again and share it in your circle if you find it useful.