73
Views
2
Comments
OnBeforeUnload event not working correctly in Reactive Application
Application Type
Reactive

I have a reactive webpage where I'd like to display a warning to the user if they have any unsaved changes before leaving the page.


The JS I have added is below


let beforeUnloadHandler = $parameters.BeforeUnloadHandler

if (!beforeUnloadHandler) {

    beforeUnloadHandler = (event) => {

        event.preventDefault();

        event.stopPropagation();


        // Included for legacy support, e.g. Chrome/Edge < 119

        event.returnValue = true;

        };

}

$parameters.Out1 = beforeUnloadHandler

let anyUnsavedChanges = $parameters.CountriesChecked

if (anyUnsavedChanges ) {

    window.removeEventListener("beforeunload", beforeUnloadHandler);

    window.addEventListener("beforeunload", beforeUnloadHandler);

} else {

    window.removeEventListener("beforeunload", beforeUnloadHandler);

}

My application has two dropdowns of which the values are passed to a web block displayed further down

The onBeforeUnload event fires correctly when attempting to exit the tab or close the window.

However if I press the back button the event fires, the warning is displayed but clicking cancel causes the 2 dropdowns to change to the empty value text, which causes the web block below them to change and lose the unsaved data.

Is anyone able to advise as to what could be the issue here?

2021-09-06 15-09-53
Dorine Boudry
 
MVP

can you share an OML demonstrating the behaviour ?

UserImage.jpg
Aaron Kashab

I have made a generic sample which demonstrates roughly how my actual application is working and pressing the back button after ticking some options also shows the issue.

OnBeforeUnloadSample.oml
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.