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 Your First JavaScript Program

Kurt Gengenbach
Kurt Gengenbach
7,994 Points

document.write does not render until AFTER second alert box is closed?

Hello all,

Using OS X 10.10.5 (Yosemite) and Safari, my document.write statement on the 2nd line does not render until AFTER I click OK on the 2nd alert statement on line 3. Not sure why my behavior is different than the instructor's video.

My code is as follows:

alert("Hello from Treehouse"); // Displays an alert box.  I click OK.
document.write("<h1>Welcome to JavaScript Basics</h1>"); // Nothing happens.
alert("Thanks for Visiting."); // Displays the second alert box.  I click OK.
// Now my document.write renders, "Welcome to JavaScript Basics"

Any idea why I am experiencing this delayed behavior?

Thanks, Kurt

//Fixed Code Presentation

3 Answers

Kurt Gengenbach
Kurt Gengenbach
7,994 Points

Chyno,

I just confirmed what you discovered. Safari renders in the wrong order (Lines 1, 3, then 2) while Chrome behaves as expected. Thanks for your help!

Kurt

Javascript works in the order that tasks are given. So you simply need to change the order of the alert and document.write.

document.write("<h1>Welcome to JavaScript Basics</h1>"); // Nothing happens.
alert("Hello from Treehouse"); // Displays an alert box.  I click OK.
alert("Thanks for Visiting."); // Displays the second alert box.  I click OK.
// Now my document.write renders, "Welcome to JavaScript Basics"

I hope this helps

Kurt Gengenbach
Kurt Gengenbach
7,994 Points

Maybe we have a misunderstanding here. I did things in the exact same order as the instructor, but even though my document.write is on the 2nd line between the two alerts, it does not render on the page until AFTER the 2nd alert is closed on line 3. So, in other words, it is occurring in the wrong order. I see it go like this: Line 1, Line 3, then Line 2.

Hmm..That's interesting. I'll see if i get the same outcome.

I am using chrome and it runs as it should on my pc. I'm wondering if it could be safari that is causing the incorrect render.

Zachary Betz
Zachary Betz
10,413 Points

Your code worked as expected on my machine. My guess is that the

alert() 

popup-box is covering up the text the

document.write("<h1>Welcome to JavaScript Basics</h1>");

line is writing, so it appears as though the code is executing out of order, when really the Welcome to JavaScript Basics text is behind the alert popup-box the whole time.