如何跨域postMessage?

发布于 2024-09-11 17:36:15 字数 1144 浏览 2 评论 0原文

postMessage 的文档暗示跨域消息传递是可能的。然而:

// When the popup has fully loaded, if not blocked by a popup blocker

这并没有非常清楚地说明如何实际做到这一点。

想象两个网站:

  1. [父网站] 托管在 qc-a.nfshost.com
  2. [子网站] 托管在 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:

  1. [Parent] hosted on qc-a.nfshost.com
  2. [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺孤伤于心 2024-09-18 17:36:15

目前,我看到两个问题。代码中的轻微错误和超时问题。

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.

羁〃客ぐ 2024-09-18 17:36:15

你正在打开窗户&互相发布消息。打开的文档不可能准备好接受邮寄消息。尝试延迟 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文