JavaScript AJAX Basics AJAX Concepts A Simple AJAX Example

Dimitris Karatsoulas
Dimitris Karatsoulas
5,787 Points

Why do we have the script tag inside head?

We've been taught through previous courses that in general, we should avoid placing the <script> tag inside the head of HTML and instead place it inside body just above </body>. I notice that's not the case in this course. Is this an exception to the rule? Placing script inside body would make same or better results for us?

2 Answers

Steven Parker
Steven Parker
203,176 Points

The standard convention was a bit oversimplified originally. Scripts that only provide libraries and framework code that is not immediately invoked will typically be loaded in the head section.

Scripts that perform a function immediately will generally be located last in the body section, or they might be wrapped in an "onload" event handler, in which case they might also be placed in the head since they won't be invoked until the entire page has loaded.