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.
Angelique SiposFront End Web Development Techdegree Student 986 Points
JS out of order in Chrome?
Hi, I'm noticing that the JS seems to be happening out of order while using Chrome.
That is my code. After refreshing, I get alerted the first time, then when I click OK, the alert with "Thanks for visiting" opens up. When I click OK then, the document.write statement gets printed on the page afterward. I'm not sure why the script seems to be running out of order.
When I view this on Firefox, each step happens accordingly. Why do you think this is happening?
Liam ClarkeTreehouse Moderator 19,873 Points
Chrome is actually doing them all in order however the actual rendering on the document write is happening after.
The reason it does this is the JS engine that each browser uses is slightly different, hence why you have different support for different versions.
In the case of the alert, it’s generally considered bad practice as it is classed as a legacy event, blocking the event loop which as you have found out is pretty unreliable.
As for what’s going on behind the scenes though, your code is actually running line by line, however rendering takes place after hoisting and after an event is fired, making your document.write get called before but not rendered until the next loop.
Nicole Antonino12,809 Points