Navigating inside Confluence does not trigger beforeunload handler in iframes

My app provides some macros. Inside those macros, users can edit some content. This content will be saved as soon as they press a save button inside the macro.

To prevent users from losing their content, I am registering a beforeunload handler that shows a warning when navigating away if there is unsaved content.

Unfortunately, when navigating away inside Confluence, it seems that the iframe is removed from the DOM without triggering a navigation event, which causes the beforeunload handler not to be triggered. This happens in particular when switching to the page editor or when switching to another page.

I haven’t been able to found a good technical implementation to manually call the beforeunload handler in the iframe. So the only solution that I can think of right now would be to use the AP bridge and provide an unload handler function there. The nice thing would be that it could allow custom unload messages. A similar thing happens right now when navigating away while there is an unsubmitted page comment.

1 Like

Hi @candid ,

I also tried a few things but didn’t get too far with this. I figure we might have to ask Confluence to provide a new Javascript API method to allow you to register to receive a notification that the macro will be closed. I’ve created CONFCLOUD-74049 for this.


1 Like