document.write () anula el contenido HTML actual. Cómo evitar esto?

I have an existing HTML file as follows-

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " html4/loose.dtd">
 <link rel="stylesheet"   type="text/css" href="chatWindow.css" />
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Chat Window</title>
   <tr> <textarea id="chatTextArea"  rows="20" cols = "80"></textarea> </tr>
   <tr> <textarea id="messageTextArea" rows="5"  cols="80"></textarea> </tr>
 <script type="text/javascript" src ="jquery-1.7.1.js" ></script>
 <script type="text/javascript" src="liveChat.js"></script>

I open this HTML in a new window from JavaScript code as follows -

 var chatWindow ="chatWindow.html", "Chat Window", "resizable=0,width=700,height=600");

Now, to this existing window, I want to add another field. I tried -

 var chatWindow ="chatWindow.html", "Chat Window", "resizable=0,width=700,height=600");
 chatWindow.document.write(' Hey!!  <input type="hidden" id="currentUserName" value="' + userName+ '"  / >  ');

But this overrode the existing HTML and so all I saw on my page was "Hey!!".

También probé

  var chatWindow ="chatWindow.html", "Chat Window", "resizable=0,width=700,height=600");
  var hiddenNode = chatWindow.document.createElement('input');
  hiddenNode.setAttribute("type", "hidden");
  hiddenNode.setAttribute("id", "currentUserName");
  hiddenNode.setAttribute("value", userName);


But this had no affect. When the new window opened, I checked its page source and the hidden node was not found. How to solve this problem? Please help.

preguntado el 09 de marzo de 12 a las 16:03

Intenta usar .appendChild in a load handler, e.g. chatWindow.onload = function() { ... }. -

Did you check what the console says? Are there any errors? -

@pimvdb Tried it. Didn't work. -

@hotS85 Console doesn't give any errors. I checked it. -

When you say you checked the 'source,' did you check the HTML source ('view source') or the DOM (with Firebug, Web Inspector, Dragonfly..)? JavaScript doesn't change the HTML of the page, just the DOM. So the hidden node might be there, if you 'inspect element' in Firebug, Web Inspector (or whichever other tool). -

3 Respuestas

You should use appendChild.

chatWindow.document.appendChild(document.createTextNode("Hey !"));

You may also use jQuery to do this more easily.

respondido 09 mar '12, 16:03

And with a div in your chatWindow ? var mydiv = chatWindow.document.getElementById("mydiv"); mydiv.appendChild(document.createTextNode("bar")); - alytrem

Try to assign properties instead of attributes.

chatWindow ="chatWindow.html"...);



If the browser opens the window in a new tab, it is accessible as well through chatWindow. (Succesfully tested with Chrome, FF and IE.)

respondido 09 mar '12, 19:03

@CodeBlue I've edited my answer. I was wrong about new tabs. The code is tested and it works. - Teemu

Trata chatWindow.document.body.appendChild(/* [...] */);.

respondido 13 nov., 12:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.