+ 3
Difference between html, css and javascript
2 odpowiedzi
+ 4
Many says it's best to place your JavaScript code on the bottom of the <body> tag. Why?
Think of your website as a human body:
Skeleton - HTML
Skin & Clothes - CSS
Muscles and Nerves - JavaScript
So, obviously, you should "compile" the skeleton first, and after you're done with the skeleton, you do the muscles and nerves. This decreases the time your browser needs to translate the code.
+ 4
JS often manipulates HTML elements it has to be loaded after HTML is rendered on the page. That means it is recommended to put Javascript that manipulate HTML elements right before closing </body> tag. There is another reason for moving JS to the bottom of the page. That is that loading JS scripts is blocking. It stops loading and rendering HTML until JS script is loaded. Although there is no difference in loading speed of the entire page, user may perceive that page is loaded faster if he gets full page design instead of the blank page while JS is loading. In case of some large JS files it may be quite noticable and annoying. There are other methods for running JS when document is ready and I am sure this course will mention that later. Simple answer is: putting JS scripts in the head section is not mandatory but head and right before closing body tag are recommended places and considered as a good practice.