28

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

aperture
  • 2,905
  • 3
  • 35
  • 58

1 Answers1

67

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

Scott Wilson
  • 1,650
  • 1
  • 17
  • 14
  • Note that IE doesn't support 'addEventListener' so use jQuery bind or someting – Matthew Lock Oct 17 '13 at 03:34
  • 4
    If you have several iframes inside each other you can replace _parent_ with _top_ to reach the top-most page: `window.top.postMessage('Hello Parent Frame!', '*');` – rlv-dan Jan 09 '15 at 07:04