2

I'm trying to take user input from one web page and write it to a different web page that already exists (all in the same domain if that matters). I debug the JavaScript (see below) and see that it iterates through the for loop correctly and builds the correct information to write, but it does not write it to the other web page. Not sure what I'm doing wrong but would greatly appreciate some help!

listitem='';

function newHTML() {

     for (i=0;i<3;i++) {
          cc=document.forms['mainform'].list[i];
          if (cc.checked) listitem+=cc.value;
     }
     HTMLstring='<HTML>\n';
     HTMLstring+='<HEAD>\n';
     HTMLstring+='<TITLE>TESTING</TITLE>\n';
     HTMLstring+='</HEAD>\n';
     HTMLstring+='<BODY bgColor="blue">\n';
     HTMLstring+='"'+listitem+'"\n';
     HTMLstring+='< /BODY>\n';
     HTMLstring+='< /HTML>';
     alert(HTMLstring);
     newwindow=window.open('writeToThisPage.html');

     newwindow.document.write(HTMLstring);
     newwindow.document.close();

     window.open('writeToThisPage.html');
}
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • It works for me in a small test app as long as I turn off pop-up blocking so that the window actually gets created. FYI, you only `document.write()` the internals of the tag (not counting the tag), you don't need all the other stuff. – jfriend00 Apr 20 '12 at 05:27

3 Answers3

2

Here's a demo. And you should avoid using document.write()

//open a new window
//"newWindow" is your reference to it
var newWindow = window.open();

//"newWindow.document.body" is the body of the new window
var newWindowBody = newWindow.document.body

//let's test by adding a text node to it
var text = document.createTextNode('foo');
newWindowBody.appendChild(text);​
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • Can you point me any good reason why not to use `document.write()` when writing to another window? In this case it's not an issue, but think about creating a `table` with 20 columns and 2000 rows, which method would you prefere? – Teemu Apr 20 '12 at 07:28
  • @Teemu there are a lot of reasons, and some of them are enumerated [here](http://stackoverflow.com/a/802943/575527) – Joseph Apr 20 '12 at 08:06
  • Hmm... I've read that post earlier. I still can't see any good reason not to use `d.w()` in cross-window actions, especially when creating whole new documents. About the XHTML-issue: I think one knows when he's using XHTML. – Teemu Apr 20 '12 at 08:21
  • @Teemu if one is to learn JS the right way, one should know what to avoid. endorsing a bad practice is like saying *"it's ok to use `eval()` because it just works"* without knowing the hazards. And the arguments in that other answer does not necessarily reflect this OP's situation. – Joseph Apr 20 '12 at 08:26
0

This is you looking for :P I hope!!!

     HTMLstring='<HTML>\n';
     HTMLstring+='<HEAD>\n';
     HTMLstring+='<TITLE>TESTING</TITLE>\n';
     HTMLstring+='</HEAD>\n';
     HTMLstring+='<BODY bgColor="green">\n';
     HTMLstring+="<p>NinaMoxa</p>\n";
     HTMLstring+="<a href=\"javascript:self.close()\">Cerrar</a>\n";
     HTMLstring+='< /BODY>\n';
     HTMLstring+='< /HTML>';
     alert(HTMLstring);


     var ventana=window.open('','name','height=400,width=500'); 
     ventana.document.write(HTMLstring);
     ventana.document.close();

By JNE

Iori Yagami
  • 134
  • 4
0

It seems all is OK, except one thing. You're opening the same window again immediately you've created the new document. Just leave this last line out:

window.open('writeToThisPage.html');
Teemu
  • 22,918
  • 7
  • 53
  • 106