JavaScript AJAX Basics (retiring) Programming AJAX Processing JSON Data

Italo Oliveira
Italo Oliveira
4,289 Points

Why isn't the script tag just right before the closing body tag?

I think I've learned somewhere that I must always set the script tag just before the closing body tag. Why in this video the script tag is inside the head tag?

2 Answers

Chris Pulver
Chris Pulver
7,622 Points

It doesn't necessarily have to be, but it is typically at the end so that the rest of the page loads before having to load the script. If there is functionality in your page that depends on the script, you would want it at the top so that it is loaded and available to use.

I didn't watch the video, but I'm guessing you are accessing something in your HTML that depends on the script.

Teacher Russell
Teacher Russell
16,867 Points

In this case, you can place it at the bottom, and the result is the same. I recommend looking up DOMContentLoaded -Event, for a way to deal with problems stemming from where the JS file script tags are located in the HTML.:)