Skip to content

Detecting iframe removed by parent

I have a web application that is embedded in an iframe by external sites, of which we have no control over.

Is there a way to detect when the iframe element has been removed from the dom of the parent, in order to perform some cleanup before it’s gone? It appears to fire a “visibilitychange” event, but I need to differentiate this between visible, hidden and removed.

Answer

The solution was listening for the "pagehide" lifecycle event. I already had a handler setup for "beforeunload" but for whatever reason, it isn’t called in this instance, and my use case was too early for "unload".