Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine current / own iframe

What is the best way to determine the iframe DOM element a page is being displayed in ?

I need to notify the parent window when the iframe's contents have really finished loading (iframe onload looks like a scam with WebKit).

Consider the following scenario:

  • Parent window loads a new iframe
  • The loaded iframe invokes a function from the parent window and sends along a reference to its iframe DOM element (sort of a "sender" parameter).
  • The parent's invoked function destroys the iframe.

Suggestions on alternative solutions to this inter-frame communication are also welcome.

One problem I faced was that Safari / Chrome / WebKit browsers apparently fire the iframe's onload event too early – I tried sending form data (with file upload) to an iframe and had the onload handler clear the <input type="file" /> element's value and destroy the iframe, causing the form submission to be aborted, so I cannot use onload in that case (btw, is that a feature or a bug ?).

Another use case is where the iframe does something and wants interact with the parent.

like image 979
Arc Avatar asked Mar 10 '26 00:03

Arc


2 Answers

Like Stefaan id say window.parent but you should check window.frameElement too.

like image 141
Knu Avatar answered Mar 12 '26 13:03

Knu


With window.parent you can get the parent window. You can also call methods on it. So with the following code you can notifiy the iframe parent that you have finished loading (you need to have a notifyLoaded function in the parent window.

window.parent.notifyLoaded(window);

Because we gave our own window the parent can loop all known iframes and check which one is loaded (using iframe.contentWindow to get the window of a certain iframe)

like image 39
Stefaan Colman Avatar answered Mar 12 '26 15:03

Stefaan Colman



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!