Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

When is it best to declare javascript SRC file, at the top of HTML or before the closing /body tag? and why?

When is it better to declare a javascript file?

One way is to declare it upfront before the <body> tag, in the <head> section.

Another is to declare the source file right before you use any of the code in the src; right before the closing tag </body>.

Assuming that in both cases, you will ensure that your javascript code will only be executed after the page (document) has finished loading and is ready.

user-image
Question added by Ahmad Al-Mutawa , Lead Engineer, System Application , Saudi Iron & Steel Company (HADEED)
Date Posted: 2016/12/12
Jonathan de Flaugergues
by Jonathan de Flaugergues , software engineer , Abbeal

The best practice is to put javascript sources files before the closing body tag not to prevent the loading of the page. So, with js at the end of the html will allow to load the content (HTML & CSS) first and not to get waiting the user before display the entire page.

More Questions Like This