使用postmessage刷新iframe的父文档

发布于 2024-09-24 17:01:47 字数 275 浏览 0 评论 0原文

我有一个greasemonkey 脚本,它打开一个iframe,其中包含来自不同子域的表单作为父页面。

我想在表单提交后刷新 iframe 时刷新父页面

我现在可以在 iframe 刷新时执行一个函数,但无法让该函数影响父文档。

我知道这是由于浏览器安全模型造成的,并且我一直在阅读有关使用 postMessage 在两个窗口之间进行通信的内容,但我似乎无法弄清楚如何用它向父级发送重新加载调用。

任何关于如何做到这一点的建议都会非常有帮助

谢谢

I have a greasemonkey script that opens an iframe containing a form from a different sub-domain as the parent page.

I would like to refresh the parent page when the iframe refreshes after the form submission

I am at the point where I can execute a function when the iframe refreshes, but I cannot get that function to affect the parent document.

I understand this is due to browser security models, and I have been reading up on using postMessage to communicate between the two windows, but I cannot seem to figure out how to send a reload call to the parent with it.

Any advice on how to do that would be very helpful

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

所有深爱都是秘密 2024-10-01 17:01:47

使用:

window.parent.postMessage('Hello Parent Frame!', '*');

注意“*”表示“任何来源”。如果可能的话,您应该将其替换为目标原点。

在您的父框架中,您需要:

window.addEventListener('message', receiveMessage, false);

function receiveMessage(evt)
{
  if (evt.origin === 'http://my.iframe.org')
  {
    alert("got message: "+evt.data);
  }
}

将“my.iframe.org”替换为 iFrame 的来源。 (您可以跳过来源验证,只需非常小心地处理您获得的数据即可)。

Use:

window.parent.postMessage('Hello Parent Frame!', '*');

Note the '*' indicates "any origin". You should replace this with the target origin if possible.

In your parent frame you need:

window.addEventListener('message', receiveMessage, false);

function receiveMessage(evt)
{
  if (evt.origin === 'http://my.iframe.org')
  {
    alert("got message: "+evt.data);
  }
}

Replace "my.iframe.org" with the origin of your iFrame. (You can skip the origin verification, just be very careful what you do with the data you get).

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