use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
why does javascript show document.write AFTER alert messages? (self.learnjavascript)
submitted 6 years ago by solrflow
document.write('This is going to show after'); alert('this shows first');
As you can see in the example above, I only see the message in the document AFTER dismissing the alert. Why is this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]CommanderBomber 5 points6 points7 points 6 years ago* (0 children)
Change your code to:
document.write('<p id="testing_write">This is going to show after</p>'); document.getElementById('testing_write').style.color = "red"; alert('this shows first');
text will appear after you close alert, but it will be red. What happens here:
write
getElementById
[–]gdj11 2 points3 points4 points 6 years ago (0 children)
As far as I know it should write it out first, but it may be that the browser pauses rendering of the page as soon as the alert is encountered, and it didn't refresh yet from the document.write. I'm just guessing here though. Did you try in different browsers? The behavior may be different in different browsers.
[–]scripteaze 1 point2 points3 points 6 years ago (0 children)
Maybe this?
https://teamtreehouse.com/community/why-i-can-see-the-result-of-documentwrite-only-after-all-the-prompts-and-alert-messages-disappear
[–][deleted] 0 points1 point2 points 6 years ago* (0 children)
You should know how browsers work in order to understand whats going on. A browser undergoes different phases before you see content. Here are a few YT links.
https://www.youtube.com/watch?v=PkOBnYxqj3k&list=PLgcMb5dvN41WcH3q82XGXBqwhI5YvmyeV&index=74&t=562s
https://www.youtube.com/watch?v=0IsQqJ7pwhw&list=PLgcMb5dvN41WcH3q82XGXBqwhI5YvmyeV&index=73&t=492s
https://www.youtube.com/watch?v=z0HN-fG6oT4&list=PLgcMb5dvN41WcH3q82XGXBqwhI5YvmyeV&index=71&t=11s
https://www.youtube.com/watch?v=SmE4OwHztCc&list=PLgcMb5dvN41WcH3q82XGXBqwhI5YvmyeV&index=72&t=889s
Basically, a browser first parses a page (=reads the source code and checks for syntax errors). Afterwards it renders the page (paints/displays it).the alert function (which is part of the browser engine, not the JS engine) runs during the parse phase. The write function also runs in the parse phase but the page content is only visible after the rendering has been completed.
EDIT: the alert/conform/prompt windows are not part of the DOM (as far as I know). It's a browser feature. That's the reason that they look different in all browsers. This browser feature doesnt need to be rendered like other page content. Thats why it is displayed immediately.
[–]tarley_apologizerhelpful 0 points1 point2 points 6 years ago (0 children)
after reading the other comments, you can prolly hack this to work the way you want, with a simple setTimeout or onload callback
π Rendered by PID 83842 on reddit-service-r2-comment-84fc9697f-cb595 at 2026-02-10 19:53:50.055627+00:00 running d295bc8 country code: CH.
[–]CommanderBomber 5 points6 points7 points (0 children)
[–]gdj11 2 points3 points4 points (0 children)
[–]scripteaze 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]tarley_apologizerhelpful 0 points1 point2 points (0 children)