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) Storing and Tracking Information with Variables Capturing Visitor Input

Jesse Dispoto
.a{fill-rule:evenodd;}techdegree seal-36
Jesse Dispoto
Front End Web Development Techdegree Graduate 14,538 Points

Why is the prompt loading before the html on the page loads?

The source.js file is placed on the bottom of the html document. Why is the prompt displaying before the page loads? Shouldn't it prompt after the page loads, according to the order in the html?

2 Answers

Jamin Pratt
Jamin Pratt
Full Stack JavaScript Techdegree Student 1,920 Points

on Google

// Once the browser has received the document (html file) it reads it. The web browser looks at the entire HTML document and looks for any css, javascript and images that are referenced by the page. If resources are found in the HTML the web browser then requests each of those resource files from the webserver. //

"The web browser looks at the entire HTML document"

It would seem that a browser looks at the entirety of the document and elements before loading. so when the prompt command comes it gets stuck and has to collect it first. if you want it to wait you would need to make it only prompt after the page is loaded... there are a few ways on the internet

Ismayil Aliyev
Ismayil Aliyev
18,560 Points

It also depends on the browser. On Edge it also shows the HTML code while running Prompt and Alert whereas on Chrome HTML is seen only after the execution of the JS.