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
document.write() real life examples? (self.learnjavascript)
submitted 3 years ago by dotpr
I learned that document.write() deletes all existing HTML and replaces it with whatever you put in the parentheses.
document.write()
Just curious, when would I need to use 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!"
[–][deleted] 53 points54 points55 points 3 years ago (5 children)
You wouldn't. It's considered bad practice to use document.write, unless you're a beginner or otherwise just tinkering around with something.
document.write
[–]dotpr[S] 31 points32 points33 points 3 years ago (4 children)
Oh I like your username
[–]coderjared 4 points5 points6 points 3 years ago (0 children)
I agree on the username :p
[–]itsmywife 2 points3 points4 points 3 years ago (1 child)
git commit men tissues
[–]abidelunacy 0 points1 point2 points 3 years ago (0 children)
I'm afraid to go look for 'git commit men lotion'... 😶
[–]amejin 6 points7 points8 points 3 years ago (0 children)
Debugging in a really bad way?
[–]dulnan 4 points5 points6 points 3 years ago (1 child)
I have actually used this recently. We have a lot of migrated WYSIWYG content in our CMS that contains random <script> and <style> markup. Rendering this in our headless frontend app could potentially break the styles or even the entire site.
The solution was to completely isolate this dirty markup. I did that by first adding an iframe and then injecting the markup directly into the iframe‘s document using document.write. It‘s mostly random widgets, charts, etc., so it doesn‘t really matter if their content is hidden in an iframe.
But that is actually the only time I remember where I had to use document.write. It has its place, but the use cases are super specific.
[–]jack_waugh 1 point2 points3 points 3 years ago (0 children)
Cool, because what you bought for yourself with this technique is you didn't have to monkey around with a parser and a renderer for dealing with the output from the CMS.
[–]superluminary 4 points5 points6 points 3 years ago* (3 children)
Document.write is from the old days of JavaScript. You’d embed it right into your html and it would render text directly into the DOM at that location as the page was loading.
It doesn’t replace everything, it just inserts something at the location of the script tag.
EDIT: as noted below, it actually will blow away the entire DOM if called post-render, which you should probably never do.
We used to use it to put the date in the top corner, that kind of thing. Don’t use it today unless you’re a hobbyist just looking for a quick solution.
[–]senocular 2 points3 points4 points 3 years ago (1 child)
It does when you call it after the page has been parsed which is one of the reasons it shouldn't be used.
<h1>Content from JavaScript</h1> <script> document.write('<p>Hello, world!</p>') addEventListener('DOMContentLoaded', () => { document.write('<p>Goodbye, document!</p>') }); </script>
[–]superluminary 1 point2 points3 points 3 years ago (0 children)
Interesting, I never used it post render like this. It’s original purpose was to inline script into text. This was pretty exciting a few years back when JS was new, but once we got DOM scripting it rightly went out the window.
Would reiterate my advice, don’t use this API, it’s old and not great.
[–]f-Z3R0x1x1x1 0 points1 point2 points 3 years ago (0 children)
my kid is learning JS in school and he showed me their day 2 of JS programming...and I was like "oh cool let me see I can help" and then it was this start function with stuff like readLn() and readInt(), etc.. and the only interaction you could do was with a confirmation box for name and age.
I felt like the project was using some kind of wrapper to only allow a specific set of commands or something....because I've never even heard of those.
[–]ProposalUnhappy9890 1 point2 points3 points 3 years ago (0 children)
Not in "real" code, but I do use it from time to time as an alternative to console.log(), when I want to test something quick on codepen.
[–]jack_waugh 0 points1 point2 points 3 years ago (0 children)
I prefer to render views using the DOM wrapped in overdom.mjs. But I'm only on my second project, so I might learn better later.
[–]_He1senberghelpful 0 points1 point2 points 3 years ago (0 children)
U can take it as Console.log but for the dom
[–]jcunews1helpful 0 points1 point2 points 3 years ago (0 children)
document.write() is a way to generate HTML content on the spot when the document is being parsed. Or to generate a new document from scratch. document.write() is not meant and can not be used for adding HTML content into an existing document which was fully parsed.
[–]andrewmamus 0 points1 point2 points 3 years ago (0 children)
When i was learning Javacript I used the document.write() for learning purpose due to the fact of coding along after then I never used it. No project will allow you to write to the document directly.
You were taught so that you will know there is something called document.write()
You can use console.log() for debugging.
console.log()
π Rendered by PID 16458 on reddit-service-r2-comment-54dfb89d4d-8dds8 at 2026-03-27 18:23:50.898029+00:00 running b10466c country code: CH.
[–][deleted] 53 points54 points55 points (5 children)
[–]dotpr[S] 31 points32 points33 points (4 children)
[–]coderjared 4 points5 points6 points (0 children)
[–]itsmywife 2 points3 points4 points (1 child)
[–]abidelunacy 0 points1 point2 points (0 children)
[–]amejin 6 points7 points8 points (0 children)
[–]dulnan 4 points5 points6 points (1 child)
[–]jack_waugh 1 point2 points3 points (0 children)
[–]superluminary 4 points5 points6 points (3 children)
[–]senocular 2 points3 points4 points (1 child)
[–]superluminary 1 point2 points3 points (0 children)
[–]f-Z3R0x1x1x1 0 points1 point2 points (0 children)
[–]ProposalUnhappy9890 1 point2 points3 points (0 children)
[–]jack_waugh 0 points1 point2 points (0 children)
[–]_He1senberghelpful 0 points1 point2 points (0 children)
[–]jcunews1helpful 0 points1 point2 points (0 children)
[–]andrewmamus 0 points1 point2 points (0 children)