2

我正在尝试从一个网页获取用户输入并将其写入另一个已经存在的网页(如果重要的话,都在同一个域中)。我调试 JavaScript(见下文)并看到它正确地遍历 for 循环并构建要写入的正确信息,但它不会将其写入其他网页。不知道我做错了什么,但非常感谢一些帮助!

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

3 回答 3

2

这是一个演示。你应该避免使用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);​
于 2012-04-20T05:28:41.947 回答
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');
于 2012-04-20T07:39:51.707 回答
0

这是你在寻找:PI 希望!!!

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

通过 JNE

于 2012-04-20T05:41:28.757 回答