简体   繁体   English

Windows之间使用JavaScript的通信

[英]Communication between windows using javascript

I've been looking for how to communicate two differents windows with Javascript, but I haven't found anything usefull... 我一直在寻找如何使用Javascript交流两个不同的窗口,但是我还没有发现任何有用的方法...

The thing is: 事情是:

Imagine that I have a Web Page which have some data and another web page is required to do something, I want to send information from the first one to the second one, process that data in the second one and get back a result to the original web page. 想象一下,我有一个包含一些数据的网页,而另一个网页需要执行某项操作,我想将信息从第一个发送到第二个,在第二个中处理该数据,然后将结果返回到原始数据。网页。

I don't know if I do make myself clear, first of all because I'm English learner, but if you can help I really appreciate that. 我不知道我是否明确表示自己,首先是因为我是英语学习者,但是如果您能帮助我,我将非常感谢。

Thx. 谢谢。

If one window is opening up the other window then you can have them pass data via function calls. 如果一个窗口正在打开另一个窗口,则可以让它们通过函数调用传递数据。 The calling window can save the window variable and then call methods on that window. 调用窗口可以保存window变量,然后在该窗口上调用方法。 The called window can also make function calls on the parent window that opened it. 被调用窗口还可以在打开它的父窗口上进行函数调用。

//the parent window must open the child window for this to work
<script>
var childWindow = null;  //page level variable for the child window

function OpenChildWindow() {
    childWindow = window.open(URL);  //create the child window and save the window to our variable
}

function SendChildData(data) {
    childWindow.XferData(data);  //this calls the function on the child window
}

function ReceiveChildData(data) {
    //do something with the data from the child window
}
</script>

//from the child window you can send or receive data as well
//the parent window is accessable via the opener property.
<script>
function SendParentData(data) {
  opener.ReceiveChildData(data)  //this calls the javascript function on the parent window and send it the data
}

function XferData(data) {
    //this receives data from the parent window
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM