admin管理员组文章数量:1316831
I'm trying to set up a simple postmessage munication between a parent window and an iframe contained inside it. I got this code on the parent side:
var ifr = document.getElementById("ifr");
ifr.contentWindow.postMessage('hello child, this is parent', '*');
window.addEventListener('message', function (e) { console.log(e.data) });
And on the child side I have:
window.parent.postMessage('hello parent, this is child', '*')
window.addEventListener('message', function (e) {console.log(e.data) } );
I receive the message sent by the iframe but not the one sent by the parent, I've checked and the iframe element is being correctly selected by the get. This is just for testing purposes to use on something else.
I'm trying to set up a simple postmessage munication between a parent window and an iframe contained inside it. I got this code on the parent side:
var ifr = document.getElementById("ifr");
ifr.contentWindow.postMessage('hello child, this is parent', '*');
window.addEventListener('message', function (e) { console.log(e.data) });
And on the child side I have:
window.parent.postMessage('hello parent, this is child', '*')
window.addEventListener('message', function (e) {console.log(e.data) } );
I receive the message sent by the iframe but not the one sent by the parent, I've checked and the iframe element is being correctly selected by the get. This is just for testing purposes to use on something else.
Share Improve this question edited Jul 25, 2017 at 7:48 Muddu Patil 7211 gold badge11 silver badges24 bronze badges asked Nov 22, 2013 at 11:12 user2958009user2958009 311 silver badge4 bronze badges 1- Set name to iframe and send message to iframe using window.frames[<index/name>] – Murali Mopuru Commented Nov 23, 2013 at 16:42
1 Answer
Reset to default 8When you send message to iframe, it is not loaded yet. Use "onload" event to start munication with iframe.
ifr.onload = function() {
this.contentWindow.postMessage('hello child, this is parent', '*');
};
本文标签: javascriptParentIframe postMessage communicationStack Overflow
版权声明:本文标题:javascript - Parent-Iframe postMessage communication - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742014221a2413464.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论