Is there any bug with document.write()?









up vote
0
down vote

favorite












I had my code something like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);





And I wanted to write something more, I added a new line.



Like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");





And then, suddenly, my input tag disappeared.



So, I wrote 'AAA'to check whether the <br> code was added or not.



Like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);

document.write("<br>");
document.write("AAA")





There's a new line, and AAA.



So, I think there's an bug or something with document.write().



Is there any bug?



Or did I write something wrong?



My purpose is to add a new line.



I want to make my code run properly.



Is there any way to solve this problem?










share|improve this question























  • Welcome to Stack Overflow, um…, so what's the problem? Your <input> being overwritten by document.write()?
    – K._
    Nov 11 at 5:55










  • @K._ No. My problem is that I can't add new line. I want to add new line and write something, but when I write document.write("<br>"), the prior code disappears.
    – Hoseong Jeon
    Nov 11 at 5:56














up vote
0
down vote

favorite












I had my code something like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);





And I wanted to write something more, I added a new line.



Like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");





And then, suddenly, my input tag disappeared.



So, I wrote 'AAA'to check whether the <br> code was added or not.



Like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);

document.write("<br>");
document.write("AAA")





There's a new line, and AAA.



So, I think there's an bug or something with document.write().



Is there any bug?



Or did I write something wrong?



My purpose is to add a new line.



I want to make my code run properly.



Is there any way to solve this problem?










share|improve this question























  • Welcome to Stack Overflow, um…, so what's the problem? Your <input> being overwritten by document.write()?
    – K._
    Nov 11 at 5:55










  • @K._ No. My problem is that I can't add new line. I want to add new line and write something, but when I write document.write("<br>"), the prior code disappears.
    – Hoseong Jeon
    Nov 11 at 5:56












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I had my code something like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);





And I wanted to write something more, I added a new line.



Like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");





And then, suddenly, my input tag disappeared.



So, I wrote 'AAA'to check whether the <br> code was added or not.



Like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);

document.write("<br>");
document.write("AAA")





There's a new line, and AAA.



So, I think there's an bug or something with document.write().



Is there any bug?



Or did I write something wrong?



My purpose is to add a new line.



I want to make my code run properly.



Is there any way to solve this problem?










share|improve this question















I had my code something like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);





And I wanted to write something more, I added a new line.



Like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");





And then, suddenly, my input tag disappeared.



So, I wrote 'AAA'to check whether the <br> code was added or not.



Like this:






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);

document.write("<br>");
document.write("AAA")





There's a new line, and AAA.



So, I think there's an bug or something with document.write().



Is there any bug?



Or did I write something wrong?



My purpose is to add a new line.



I want to make my code run properly.



Is there any way to solve this problem?






window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);





window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);





window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");





window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");





window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);

document.write("<br>");
document.write("AAA")





window.onload = function() 
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);

document.write("<br>");
document.write("AAA")






javascript onload






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 5:57









Tân Nguyễn

1




1










asked Nov 11 at 5:50









Hoseong Jeon

22412




22412











  • Welcome to Stack Overflow, um…, so what's the problem? Your <input> being overwritten by document.write()?
    – K._
    Nov 11 at 5:55










  • @K._ No. My problem is that I can't add new line. I want to add new line and write something, but when I write document.write("<br>"), the prior code disappears.
    – Hoseong Jeon
    Nov 11 at 5:56
















  • Welcome to Stack Overflow, um…, so what's the problem? Your <input> being overwritten by document.write()?
    – K._
    Nov 11 at 5:55










  • @K._ No. My problem is that I can't add new line. I want to add new line and write something, but when I write document.write("<br>"), the prior code disappears.
    – Hoseong Jeon
    Nov 11 at 5:56















Welcome to Stack Overflow, um…, so what's the problem? Your <input> being overwritten by document.write()?
– K._
Nov 11 at 5:55




Welcome to Stack Overflow, um…, so what's the problem? Your <input> being overwritten by document.write()?
– K._
Nov 11 at 5:55












@K._ No. My problem is that I can't add new line. I want to add new line and write something, but when I write document.write("<br>"), the prior code disappears.
– Hoseong Jeon
Nov 11 at 5:56




@K._ No. My problem is that I can't add new line. I want to add new line and write something, but when I write document.write("<br>"), the prior code disappears.
– Hoseong Jeon
Nov 11 at 5:56












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










The problem is that, if the page has already loaded, document.write will replace the entire page with the new HTML string. If you removed your window.onload handler and simply had that snippet run as soon as the browser comes to the <script> tag, both the <input> and <br> would be inserted into the document as expected:






<script language="javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");
</script>





Generally, the solution is to not use document.write - its behavior can be confusing, and it provides nothing that can't be accomplished just as easily with methods such as appendChild and insertAdjacentHTML, for example:



document.body.appendChild(document.createElement('br'));


or



document.body.insertAdjacentHTML('beforeend', '<br>');





share|improve this answer






















  • Thank you for quick answer. Then is there any way to set that when the page is loaded?
    – Hoseong Jeon
    Nov 11 at 5:55










  • Don't use document.write after the page is loaded, because then the page will be replaced - use other methods like appendChild and insertAdjacentHTML instead, there's no way around it.
    – CertainPerformance
    Nov 11 at 5:56











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246198%2fis-there-any-bug-with-document-write%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










The problem is that, if the page has already loaded, document.write will replace the entire page with the new HTML string. If you removed your window.onload handler and simply had that snippet run as soon as the browser comes to the <script> tag, both the <input> and <br> would be inserted into the document as expected:






<script language="javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");
</script>





Generally, the solution is to not use document.write - its behavior can be confusing, and it provides nothing that can't be accomplished just as easily with methods such as appendChild and insertAdjacentHTML, for example:



document.body.appendChild(document.createElement('br'));


or



document.body.insertAdjacentHTML('beforeend', '<br>');





share|improve this answer






















  • Thank you for quick answer. Then is there any way to set that when the page is loaded?
    – Hoseong Jeon
    Nov 11 at 5:55










  • Don't use document.write after the page is loaded, because then the page will be replaced - use other methods like appendChild and insertAdjacentHTML instead, there's no way around it.
    – CertainPerformance
    Nov 11 at 5:56















up vote
3
down vote



accepted










The problem is that, if the page has already loaded, document.write will replace the entire page with the new HTML string. If you removed your window.onload handler and simply had that snippet run as soon as the browser comes to the <script> tag, both the <input> and <br> would be inserted into the document as expected:






<script language="javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");
</script>





Generally, the solution is to not use document.write - its behavior can be confusing, and it provides nothing that can't be accomplished just as easily with methods such as appendChild and insertAdjacentHTML, for example:



document.body.appendChild(document.createElement('br'));


or



document.body.insertAdjacentHTML('beforeend', '<br>');





share|improve this answer






















  • Thank you for quick answer. Then is there any way to set that when the page is loaded?
    – Hoseong Jeon
    Nov 11 at 5:55










  • Don't use document.write after the page is loaded, because then the page will be replaced - use other methods like appendChild and insertAdjacentHTML instead, there's no way around it.
    – CertainPerformance
    Nov 11 at 5:56













up vote
3
down vote



accepted







up vote
3
down vote



accepted






The problem is that, if the page has already loaded, document.write will replace the entire page with the new HTML string. If you removed your window.onload handler and simply had that snippet run as soon as the browser comes to the <script> tag, both the <input> and <br> would be inserted into the document as expected:






<script language="javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");
</script>





Generally, the solution is to not use document.write - its behavior can be confusing, and it provides nothing that can't be accomplished just as easily with methods such as appendChild and insertAdjacentHTML, for example:



document.body.appendChild(document.createElement('br'));


or



document.body.insertAdjacentHTML('beforeend', '<br>');





share|improve this answer














The problem is that, if the page has already loaded, document.write will replace the entire page with the new HTML string. If you removed your window.onload handler and simply had that snippet run as soon as the browser comes to the <script> tag, both the <input> and <br> would be inserted into the document as expected:






<script language="javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");
</script>





Generally, the solution is to not use document.write - its behavior can be confusing, and it provides nothing that can't be accomplished just as easily with methods such as appendChild and insertAdjacentHTML, for example:



document.body.appendChild(document.createElement('br'));


or



document.body.insertAdjacentHTML('beforeend', '<br>');





<script language="javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");
</script>





<script language="javascript">
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", "");
document.body.appendChild(x);
document.write("<br>");
</script>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 5:56

























answered Nov 11 at 5:54









CertainPerformance

67.9k143353




67.9k143353











  • Thank you for quick answer. Then is there any way to set that when the page is loaded?
    – Hoseong Jeon
    Nov 11 at 5:55










  • Don't use document.write after the page is loaded, because then the page will be replaced - use other methods like appendChild and insertAdjacentHTML instead, there's no way around it.
    – CertainPerformance
    Nov 11 at 5:56

















  • Thank you for quick answer. Then is there any way to set that when the page is loaded?
    – Hoseong Jeon
    Nov 11 at 5:55










  • Don't use document.write after the page is loaded, because then the page will be replaced - use other methods like appendChild and insertAdjacentHTML instead, there's no way around it.
    – CertainPerformance
    Nov 11 at 5:56
















Thank you for quick answer. Then is there any way to set that when the page is loaded?
– Hoseong Jeon
Nov 11 at 5:55




Thank you for quick answer. Then is there any way to set that when the page is loaded?
– Hoseong Jeon
Nov 11 at 5:55












Don't use document.write after the page is loaded, because then the page will be replaced - use other methods like appendChild and insertAdjacentHTML instead, there's no way around it.
– CertainPerformance
Nov 11 at 5:56





Don't use document.write after the page is loaded, because then the page will be replaced - use other methods like appendChild and insertAdjacentHTML instead, there's no way around it.
– CertainPerformance
Nov 11 at 5:56


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246198%2fis-there-any-bug-with-document-write%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3