Search code examples
javascriptiframeparentonbeforeunload

onbeforeunload not fired when the iframe containing the page is removed


I have a webpage Page1 that defines a working onbeforeunload event in which I do some fetch. This Page1 is loaded in an iframe1 on another domain.

Problem: When the iframe1 is removed from DOM my Page1 beforeunload event isn't fired at all.

I'm trying locally right now with those files:

page1.html:

<script>
window.onbeforeunload = () => {
  console.log('Page1 unloaded')
}
</script>

parent.html:

<div>
    <iframe id='page1' src='./page1.html' ></iframe>
</div>
<button onclick="document.getElementById('page1').remove()" >Remove</button>

Question: How can I fire the unload event of my Page1 ?

Thanks in advance.


Solution

  • Finally, I ended setting up a websocket on my back-end. I was on nodejs env, I used socket.io.