Saturday 25 February 2017

javascript - Advantages of script tag before closing body tag when using events

These days it seems like people recommend placing the script tag just before the closing body tag as such..


        



If you are running the script immediately, this is a good thing because most of your DOM has mostly loaded. However, what if you are using the onload or DOMContentLoaded events to call your main script? In this case, it seems that it would make no difference if placing your script tag in the document head, since your code will not execute anyways until the DOM loads.


When using the onload or DOMContentLoaded events to run your script, is there really any advantage to putting your script tag above the closing body tag vs the head?


On the other hand, if you don't want your script to execute until DOM has loaded, why not simply place your script tag before the closing body tag and run it immediately without any events whatsoever. Seems like this would save a few lines of code while providing the same functionality.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...