Confirmation dialog on page close breaks Collabora doc

Hi,
in our scenario we need to ask the user for confirmation before the document browser window is closed.
In order to do so we manage the PostMessage “modified status” message and handle the beforeunload event, as per Window: beforeunload event - Web APIs | MDN

We register an handler

window.addEventListener("beforeunload", handleBeforeUnload);

function handleBeforeUnload(e) {
    if(postmessage_modifiedStatus) e.preventDefault();
}

By doing so the confirmation dialog appears if I try to close the browser, but if I click “Cancel” and decide to keep the page opened and abort the browser close, something in the document breaks and it’s reloaded from scratch.

Is there a correct way to ask for confirmation before closing a Collabora page? Or is it impossible to open the beforeunload confirmation dialog with Collabora?

Thank you for any support you can provide

Nevermind, it looks like the problem only happens when launching the application (.NET / Blazor app) in debug mode in Visual Studio 2022.
It works fine deploying the application on a test server.

Awesome @LucaDuttoTD. BTW, thanks for sharing this. It will help others as well…

Thanks,
Darshan

1 Like