如何使用JavaScript写入其他网页? [英] How do I use JavaScript write to a different web page?

查看:101
本文介绍了如何使用JavaScript写入其他网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

这是一个演示.而且您应该避免使用document.write()

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

这篇关于如何使用JavaScript写入其他网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆