Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,624 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,873 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.:)