Search code examples
javascriptdomiframedom-eventsonload

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.


Solution

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