How do I use JavaScript write to a different web page?

萝らか妹 提交于 2020-05-13 14:48:12

问题


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:


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



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/10240669/how-do-i-use-javascript-write-to-a-different-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!