Stop webpage from closing

I want to stop a webpage from closing if a user has not saved the entries. This should happen if they close the browser or close the tab. Is it possible to stop the tab or browser from closing? I don’t see anything like cancelclose. Thanks

You can do this with some JavaScript, with the Window.beforeunload event.

It will work just fine in Desktop browsers (I’ve tested it on Safari, Chrome, Firefox and Edge). Android as well (tested on Chrome, Edge, Firefox and Samsung Browser). However, iOS will just ignore it (tested on Safari and Chrome).

Where would I add that JavaScript function in my web app?
All of our users are on Edge of Firefox so It should work for them. I will try and see if I can make Window.beforeunload work.

Almost got it working. I added this to the textchanged event of a webtextfield and a message shows when I try to close the tab.

if javaadded = false then
ExecuteJavaScript(“window.addEventListener(‘beforeunload’, (event) => {event.preventDefault(); event.returnValue = ‘’;});”)
javaadded = true
end if

But how do I remove the EventListener? I tried this at the end of the save method, but it did Not remove the EventListener.

ExecuteJavaScript(“window.removeEventListener(‘beforeunload’, (event) => {event.preventDefault(); event.returnValue = ‘’;});”)
javaadded = false

Try creating the listener function as a variable.

As suggested here

2 Likes