如何跨域postMessage?
postMessage 的文档暗示跨域消息传递是可能的。然而:
// When the popup has fully loaded, if not blocked by a popup blocker
这并没有非常清楚地说明如何实际做到这一点。
想象两个网站:
- [父网站] 托管在
qc-a.nfshost.com
上 - [子网站] 托管在
qc-b.quadhome.com
在父网站中:
document.addEventListener('message', function(e) {
alert('Parent got (from ' + e.origin + '): ' + e.data);
e.source.postMessage('Round-tripped!', 'http://qc-b.quadhome.com');
}, false);
function go() {
var w = window.open('http://qc-b.quadhome.com', 'test');
/* This doesn't work because same-origin policy prevents knowing when
the opened window is ready. */
w.postMessage('Vain attempt.', 'http://qc-b.quadhome.com');
}
并且,在孩子:
document.addEventListener('message', function(e) {
alert('Child got (from ' + e.origin + '): ' + e.data);
}, false);
window.opener.postMessage('Ready!', 'http://qc-a.nfshost.com');
一切都无济于事。
帮助?
The documentation for postMessage implies that cross-domain messaging is possible. However:
// When the popup has fully loaded, if not blocked by a popup blocker
That isn't a very clear note of how to actually do it.
Imagine two websites:
- [Parent] hosted on
qc-a.nfshost.com
- [Child] hosted on
qc-b.quadhome.com
In the parent:
document.addEventListener('message', function(e) {
alert('Parent got (from ' + e.origin + '): ' + e.data);
e.source.postMessage('Round-tripped!', 'http://qc-b.quadhome.com');
}, false);
function go() {
var w = window.open('http://qc-b.quadhome.com', 'test');
/* This doesn't work because same-origin policy prevents knowing when
the opened window is ready. */
w.postMessage('Vain attempt.', 'http://qc-b.quadhome.com');
}
And, in the child:
document.addEventListener('message', function(e) {
alert('Child got (from ' + e.origin + '): ' + e.data);
}, false);
window.opener.postMessage('Ready!', 'http://qc-a.nfshost.com');
All to no avail.
Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,我看到两个问题。代码中的轻微错误和超时问题。
1)我在您的代码中看到的错误是您正在使用 document.addEventListener。我认为正确的一个是window.addEventListener。它位于 postMessage 页面上的示例中。
2)随着超时,你可以让子窗口向父窗口postMessage。然后父窗口就会知道子窗口何时准备好。
Currently, I am seeing two issues. Slight error in the code and the timeout issue.
1) The error I am seeing in your code is that you're using document.addEventListener. I think the right one is window.addEventListener. It is in the example on the postMessage page.
2) With the timeout, you can have the child window postMessage to the parent. The parent window will then know when the child is ready.
你正在打开窗户&互相发布消息。打开的文档不可能准备好接受邮寄消息。尝试延迟 postMessage 调用,直到窗口完成加载。
测试这一点的一个非常简单的方法是将 w.postMessage() 包装在 setTimeout 中(10 秒),并查看当文档准备好时它是否可以发布它。
You're opening the window & posting the message after each other. There's no way the open document will be ready to accept the post message. Try delaying the postMessage call until the window has finished loading.
A very simple way to test this is to wrap w.postMessage() in a setTimeout (for 10 seconds) and see if it can post it when the document is ready.