Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

cross domain - using postmessage to refresh iframe's parent document

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...