Close iframe and refresh parent cross domain

2020-03-04 07:06发布

问题:

I have an iframe that loads on someone else's page. When the iframe is closed I would like to refresh the parent page.

I currently employ a hash hack similar to what's described here: Close iframe cross domain

This method gives security problems in IE9 though, so I'm still looking for better solutions or an IE workaround..

Any ideas?

Related: IFrame on unload refresh parent page

回答1:

I think the only reliable way is to use postMessage in modern browsers for cross-domain communication, have not tested it but check this code part out... of course this only works if the "someone else's page" is somehow changeable

so inside if your page you do:

var parent = window.parent;
if (parent && parent.postMessage) {
  parent.postMessage("closed");
}

on the other persons page:

window.onmessage = function(event) {
  if (event.data === "closed") {
    location.reload();
  }
};