Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
Jordan Avatar asked Jan 28 '26 16:01

Jordan


1 Answers

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.

like image 129
Yuriy Galanter Avatar answered Jan 30 '26 04:01

Yuriy Galanter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!