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');
}
1
  • 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 <body> tag (not counting the tag), you don't need all the other stuff. Commented Apr 20, 2012 at 5:27

3 Answers 3

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);​
Sign up to request clarification or add additional context in comments.

4 Comments

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 there are a lot of reasons, and some of them are enumerated here
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 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.
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

Comments

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');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.