如何在父窗口中获取打开的子窗口的引用?

时间:2021-07-01 09:09:31

I need to update the data on the child window from the parent window.

我需要从父窗口更新子窗口上的数据。

For this purpose I need to get the reference of the opened child window and pass the info to the child function named UpdateInfo()

为此,我需要获取打开的子窗口的引用并将信息传递给名为UpdateInfo()的子函数

Here the row that opens the child window in parent web page:

这里是在父网页中打开子窗口的行:

window.open("HTMLPage2.htm" , "child" , "width=200, height=100")

Any idea how can I get the reference of the opened child window whithin the parent window?
Thank you in advance.

任何想法如何在父窗口中获取打开的子窗口的引用?先感谢您。

P.S. I dont know if my approch is wright, so if there is a better approch please write me in the comment.

附:我不知道我的approch是否是怀特,所以如果有更好的approch请在评论中写下我。

1 个解决方案

#1


1  

window.open returns a reference of the child window. You can use something like this:

window.open返回子窗口的引用。你可以使用这样的东西:

var child = window.open("HTMLPage2.htm" , "child" , "width=200, height=100");
child.updateInfo(...);

This is possible only if the child's URL conforms to the Same-Origin Policy.

仅当子项的URL符合同源策略时,才可以执行此操作。

EDIT:
If the child is not from the same origin, you can still communicate with it via the window.postMessage() API. This enables you to send a message event to the child-window, and the child can listen to that event and invoke custom functionality.

编辑:如果孩子不是来自同一个来源,你仍然可以通过window.postMessage()API与它进行交流。这使您可以将消息事件发送到子窗口,并且子节点可以侦听该事件并调用自定义功能。

#1


1  

window.open returns a reference of the child window. You can use something like this:

window.open返回子窗口的引用。你可以使用这样的东西:

var child = window.open("HTMLPage2.htm" , "child" , "width=200, height=100");
child.updateInfo(...);

This is possible only if the child's URL conforms to the Same-Origin Policy.

仅当子项的URL符合同源策略时,才可以执行此操作。

EDIT:
If the child is not from the same origin, you can still communicate with it via the window.postMessage() API. This enables you to send a message event to the child-window, and the child can listen to that event and invoke custom functionality.

编辑:如果孩子不是来自同一个来源,你仍然可以通过window.postMessage()API与它进行交流。这使您可以将消息事件发送到子窗口,并且子节点可以侦听该事件并调用自定义功能。