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 (Retired) Introducing JavaScript Your First JavaScript Program

My "welcome to javascript basics" does not show up until after the 2nd alert box is closed.

I have typed the exact code but when i preview, the first alert shows, then the second alert shows and its not until after i close the second alert that the document.write appears on the page?

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

Would you post your JavaScript code? Then I can help you.

alert("Hello from Keanu"); document.write("<h1>welcome to javascript basics</h1>"); alert("thanks for visiting");

Thank you!

Melissa

3 Answers

Thomas Nilsen
Thomas Nilsen
14,956 Points

I tried you code and it works. To make it more clear I added some delay to the latest alert box:

alert("Hello from Keanu"); 
document.write("<h1>welcome to javascript basics</h1>"); 
setTimeout(function() {alert("thanks for visiting");}, 1000)

Thank you so much!

Melissa

megantong
megantong
1,965 Points

Hi Thomas. I'm running the latest google chrome: Version 54.0.2840.71 m. I am having the same problem as mh24. I copy and pasted your code to see if it would work but it basically does the same thing - showing the two alerts first before the document.write.
I know you posted this a few months ago, but not sure if there is another way to go about this.

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

The code is fine. I think it is a browser problem instead. I have tested the code in Chrome, Firefox and Safari. In Chrome and Firefox it works as expected, but in Safari it behaves as you described. It seems that document.write does not work correctly in Safari, so for these practice exercises using document.write I suggest using another browser. When you write production code, where it needs to work in every browser, you probably won't use document.write anyway.

Thomas Nilsen
Thomas Nilsen
14,956 Points

If you add delay on the last alert, it works in Safari too for some reason..

Inevitable Walrus
Inevitable Walrus
10,032 Points

Hm, yeah. Running most recent version of Chrome and

alert("1");
document.write("2");
alert("3");

displays 1-3-2 instead of 1-2-3.

Bit strange, but a clean workaround is to include a setTimeout.