Jordan
Jordan

Reputation: 4522

Parent redirect when using iframe

I know there are several questions/answers already about how to redirect a parent window from within an iframe, but this one has a slight twist.

I need to be able to redirect the parent window when the page in the iframe is changed. However, the page in the iframe is not on my domain, so I can't use the parent.window.location trick that was suggested in other threads.

So, for example, if I put stackoverflow.com in my iframe, I need the parent frame to redirect when the user clicks any link on the stackoverflow page.

I doubt this is actually possible, but I thought there might be some sort of polling of the iframe that the parent can do or something.

Upvotes: 4

Views: 3658

Answers (1)

Yuriy Galanter
Yuriy Galanter

Reputation: 39807

This is a bit convoluted, but should do the trick. If you define your IFRAME like this

<iframe id="xMyIframe" src="http://someurl" onload="replaceLoad(this)" ></iframe>

Then you can define a JavaScript code;

function replaceLoad(oIframe) {
  oIframe.onload = function () {
     location.href = 'http://www.microsfot.com'
  }
}

On load of the initial Iframe URL inital onload event handler fires which replaces itself with new handler that will fire after next load i.e. after user clicks link in the iframed page.

Upvotes: 2

Related Questions