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

Why is JavaScript tag is placed before body end tag not after?

Why is JavaScript tag is placed before body end tag not after?

5 Answers

That's too low:) It will result in an Invalid HTML Document. Best is to put it just inside the closing body tags. The other option is inside the head, but the browser stops loading the html temporarily to load the Javascript file, so the bottom of the body is usually best. Does that help?

Moderator Edit: Moved response from Comments to Answers, as it provides an answer and can be upvoted and/or marked Best Answers.

Sorry for the wait this time. I work long days:) Actually, I've never tried putting tags below the body, but I'm guessing how it's handled may vary from browser to browser. I'm pretty sure it should result in the code being invalid. I believe only "comments" and the closing html tags are allowed below the closing body tag. Is there a reason you wish to do this?

Thank you Russell. I got the answer that I was looking. It is not that I want to place JS code below </body>, I just wanted to know the reason.

Got it. If you feel like giving me a best answer, cool, and if not, that's ok too. I'm really glad I could help. Happy coding. Stay at it.

There is no reason to not to give it best answer. I already gave it. Thank you again.

Russell, Thank for replying so quick. Could you please explain to me, what do you mean by "It will result in an Invalid HTML Document. "?

Will I get an error on "document" keyword?