Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close iframe and refresh parent cross domain

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

like image 227
Alex Avatar asked Apr 23 '12 16:04

Alex


People also ask

How can an iframe communicate with its parent?

All you have to do is first dispatch an event from the iframe to the parent that notifies the parent that the iframe is loaded (essentially a "ready message"). The parent will be listening for messages and if it receives the "ready message" event, it can then reply to the iframe with whatever message you want to send.

Can iframe access parents?

When a page is running inside of an iframe, the parent object is different than the window object. You can still access parent from within an iframe even though you can't access anything useful on it.

Is iframe cross domain?

A cross domain inline frame (iframe) is a type of web technology that can be used to embed a small portion of one website within a larger "parent" page hosted on a different domain.

How do I transfer data from iframe to parent?

Sending some data from the child iframe to the parent window is also pretty simple. Whenever you embed an iframe, the iframe will have a reference to the parent window. You just need to use the PostMessage API to send data via the window. parent reference of the parent window.


1 Answers

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();
  }
};
like image 88
Tobias Krogh Avatar answered Sep 28 '22 20:09

Tobias Krogh