Angelique SiposFront End Web Development Techdegree Student 984 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,722 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,259 Points