Communicating cross-origin from parent to child iframe

寵の児 提交于 2019-12-10 01:16:14

问题


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.

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?


回答1:


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.

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

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



来源:https://stackoverflow.com/questions/19011780/communicating-cross-origin-from-parent-to-child-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!