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.
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?
Thomas Nilsen14,956 Points
I tried you code and it works. To make it more clear I added some delay to the latest alert box:
Benjamin Barslev Nielsen18,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.
Inevitable Walrus10,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.