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?
javascript onload
add a comment |
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?
javascript onload
Welcome to Stack Overflow, um…, so what's the problem? Your<input>
being overwritten bydocument.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 writedocument.write("<br>")
, the prior code disappears.
– Hoseong Jeon
Nov 11 at 5:56
add a comment |
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?
javascript onload
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
javascript onload
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 bydocument.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 writedocument.write("<br>")
, the prior code disappears.
– Hoseong Jeon
Nov 11 at 5:56
add a comment |
Welcome to Stack Overflow, um…, so what's the problem? Your<input>
being overwritten bydocument.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 writedocument.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
add a comment |
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>');
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 usedocument.write
after the page is loaded, because then the page will be replaced - use other methods likeappendChild
andinsertAdjacentHTML
instead, there's no way around it.
– CertainPerformance
Nov 11 at 5:56
add a comment |
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>');
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 usedocument.write
after the page is loaded, because then the page will be replaced - use other methods likeappendChild
andinsertAdjacentHTML
instead, there's no way around it.
– CertainPerformance
Nov 11 at 5:56
add a comment |
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>');
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 usedocument.write
after the page is loaded, because then the page will be replaced - use other methods likeappendChild
andinsertAdjacentHTML
instead, there's no way around it.
– CertainPerformance
Nov 11 at 5:56
add a comment |
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>');
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>
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 usedocument.write
after the page is loaded, because then the page will be replaced - use other methods likeappendChild
andinsertAdjacentHTML
instead, there's no way around it.
– CertainPerformance
Nov 11 at 5:56
add a comment |
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 usedocument.write
after the page is loaded, because then the page will be replaced - use other methods likeappendChild
andinsertAdjacentHTML
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Welcome to Stack Overflow, um…, so what's the problem? Your
<input>
being overwritten bydocument.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