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 JavaScript Basics (Retired) Introducing JavaScript Where Does JavaScript Go?

Donavon Cai
Donavon Cai
257 Points

Can't get code to run in the correct order.

I'm trying to create a page in which an alert runs, then the page loads, then another alert runs. However, the two alerts run before the page loads despite having the lines in (what I think) is the correct order. New to programming; can someone tell me what I'm doing wrong?

Here's the code:

<!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>

The code for the scripts.js file:

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

1 Answer

Steven Parker
Steven Parker
231,269 Points

It's not your program.

Modern browsers don't actually render the page until the JavaScript has completed, so any alerts will be shown while the program is running, but anything written to the page will be shown after the program ends.