trigger unload event in iframe app when parent application tab closes

205 Views Asked by At

I have two apps, App1 and App2. App2 is embedded as an iframe in App1, so App1 is parent app which embeds App2. When user closes App1 from browser, I want to trigger an api in App2. I tried using 'unload' event in App2, but that is not getting triggered when App2 closes (am checking by putting debugger point on event listener function, which gets called when standalone App2 gets closed). Is there a way to accomplish this? Thanks

1

There are 1 best solutions below

5
charlietfl On

Historically trying to make asynchronous requests in unload events has never been reliable

For that reason navigator.sendBeacon(url, data) was created.

It works in the background even after page or tab is gone and helps solve a lot of the reliability problems of using unload events.

With the sendBeacon() method, the data is transmitted asynchronously when the User Agent has an opportunity to do so, without delaying unload or the next navigation.

I've never used it in the disappearing iframe scenario like yours but believe it would solve your issue