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

Gedalyah (Gary) Frankel
PLUS
Gedalyah (Gary) Frankel
Courses Plus Student 7,234 Points

Placing JavaScript near the bottom of the page

Dave McFarland wrote the following JavaScript in the "https://teamtreehouse.com/library/javascript-basics/introducing-javascript/where-does-javascript-go" video’s scripts.js file:

alert("Hey, you're back for more?");

And the following code in the video’s index.html file:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/main.css"> <title>JavaScript Basics</title> <script> alert("Here's another message from Treehouse"); </script> </head> <body> <div class="container"> <h1>Where to place your JavaScript code.</h1> </div> <script src="scripts.js"></script> </body> </html>

Dave says:

• 4:30 See what happens if I move this script tag to the bottom of the page. (GF: See my red shape above) • 4:35 When I save this file and preview the workspace now, • 4:38 the alert in the page loads first, then the content of the page displays. • 4:43 See the headline. • 4:44 Then the alert from the scripts.js file appears.

GF: When I save this file and preview the workspace the alert in the page loads first (OK), then the ALERT from the scripts.js file appears, AND then the CONTENT of the page displays.

What Dave says makes sense, but I doesn’t happen in Workspaces. I even downloaded my Workspace and tried it in Brackets Live Preview.

Any Advice?

3 Answers

Steven Parker
Steven Parker
229,732 Points

This behavior may vary in different browser versions.

Most modern browsers do not display the page content until any script code has completed running. Older browser versions, including the one used to make the video, might display the content before the script has finished.

This should probably be mentioned in the teacher's notes. I know it is in some of the other courses. You might want to mention this to the Support folks.

Gedalyah (Gary) Frankel
PLUS
Gedalyah (Gary) Frankel
Courses Plus Student 7,234 Points

Hi Steven,

Thanks for the answer. I will drop Support an email.

As a side note I tried this out in some of the further videos by playing w/the code and the page content did display before the code was done running.

Steven Parker
Steven Parker
229,732 Points

The behavior of a particular browser version should be consistent. Perhaps the other code is specifically designed to perform concurrent operations.

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Gedalyah (Gary) Frankel

The problem you're seeing isn't related to placing the JavaScript code at the bottom of the page, but to the way browsers now handle the alert( ) statement, which has changed since I shot this course. I'll update this video soon