If I have a document.write
already in my dom as the page loads, it writes only where it's called to write. For example:
<div id="d1">
existing text <script>document.write('& doc written text');</script>
</div>
<div>footer</div>
Will render as <div id="d1">existing text & doc written text</div><div>footer</div>
However, if I were to use jQuery to update the html within div, like
$("#d1").html("another approach <script>document.write('wipes out the screen');</script>");
I end up simply with wipes out the screen
b/c it essentially gets rid of everything in the document, vs. writing only where the inline script is. How can I .html()
with a value containing document.write()
, but have it behave the same as if it were in the dom to begin with (see first example). Thanks!
If I have a document.write
already in my dom as the page loads, it writes only where it's called to write. For example:
<div id="d1">
existing text <script>document.write('& doc written text');</script>
</div>
<div>footer</div>
Will render as <div id="d1">existing text & doc written text</div><div>footer</div>
However, if I were to use jQuery to update the html within div, like
$("#d1").html("another approach <script>document.write('wipes out the screen');</script>");
I end up simply with wipes out the screen
b/c it essentially gets rid of everything in the document, vs. writing only where the inline script is. How can I .html()
with a value containing document.write()
, but have it behave the same as if it were in the dom to begin with (see first example). Thanks!
document.write()
after DOM is loaded, otherwise it will erase your entire document. Is there a reason you want to use it that way?
– ulentini
Commented
Mar 21, 2013 at 18:28
The html()
function replaces the contents of an element. (See the API)
What you want is append()
.
If the page has fully been written, you can not use document.write(). There is nothing you can do to change how it behaves. You need to rethink what you are doing.