从父母到儿童iframe交流交叉起源 [英] Communicating cross-origin from parent to child iframe

查看:100
本文介绍了从父母到儿童iframe交流交叉起源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方Javascript 。我对父页面和来自不同来源的子框架之间的通信特别感兴趣。使用window.postMessage,安全地从孩子发送消息并让父母通过消息事件接收消息是微不足道的。

I'm working through Third Party Javascript. I'm particularly interested in communication between a parent page and a child frame from a different origin. Using window.postMessage, it's trivial to safely send messages from the child and have the parent receive them with the message event.

我没有运气朝另一个方向走。我是否可以使用postMessage确认 是否可以从父级与孩子进行通信?如果不是解决这个问题的方法是什么?

I have had no luck going the other direction. Can I get some confirmation that it is not possible to communicate from the parent to the child using postMessage? If not what are ways of working around this problem?

推荐答案

你应该可以在任何一个之间使用 .postMessage()两个合作的窗户。发送消息的方向无关紧要。如果它不是朝一个方向工作,那么实现中可能存在错误。您可能需要确保拥有正确的窗口对象。

You should be able to use .postMessage() between any two cooperating windows. It doesn't matter which direction you are sending the message. If it is not working in one direction, then there is probably an error in the implementation. You may need to make sure you have the proper window object.

对于iframe,您可能希望使用 .contentWindow 在现代浏览器中获取窗口的属性。

For an iframe, you would want to use the .contentWindow property to get its window in modern browsers.

这里有一些很好的文档:https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage

Some good documentation here: https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage

这篇关于从父母到儿童iframe交流交叉起源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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