使用 postmessage 刷新 iframe 的父文档 [英] using postmessage to refresh iframe's parent document

查看:27
本文介绍了使用 postmessage 刷新 iframe 的父文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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

推荐答案

使用:

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);
  }
}

将my.iframe.org"替换为您的 iFrame 的来源.(您可以跳过来源验证,只要小心处理您获得的数据即可).

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).

这篇关于使用 postmessage 刷新 iframe 的父文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆