Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics Hello, JavaScript! Add JavaScript to HTML

Penelope Grayson
Penelope Grayson
4,939 Points

Why won't my content display before the second alert, even though the script is just before the closing <body> tag?

Near the end of the video, Guil moves the script for the second alert near the bottom of the page, just before the closing <body> tag. This results in the html content being displayed before the second alert.

This didn't work for me, does anyone know why? I am using Chrome.

I managed to get it to work by using a (complicated looking....to me) window.addEventListener event that I found on google, but I'd love to know why it didn't work like in the video.

Thanks!

1 Answer

Steven Parker
Steven Parker
215,372 Points

It's probably due to a difference in the behavior of browsers when the video was made. Modern ones defer rendering of the page until after all the JavaScript code has finished running.

Penelope Grayson
Penelope Grayson
4,939 Points

Ok, thank you very much Steven, that makes perfect sense!

Thanks for the clarification Steven.