Saturday 15 October 2016

Best place to insert JavaScript within a HTML document

Really depends on what you are trying to do.


General rule of thumb would have you put your javaScript code in a seperate file and link that script with something like this




Then again, having your script inline within the page, you can have it where you want. Within the headsection of the page, it loads beforehand, and therefore can pre-process if needed. If you have it let's say during the body portion of the page, it will load with the normal flow of the page.


Having everything on the same file, I would have the script in the head and, if needed, call functions afterwards in the body.


Hope this helps.


edit


Keep in mind that the main goal should be readability and re-usability of your code. Therefore, whatever makes this happen in the most efficient way, I would have to say is the best way.

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...