Anyone knows how to check if the user is trying to close the page/tab or navigate to another screen on a reactive web app?

I need to open a custom confirmation popup in this situations.

Regards

Just for curiosity sake - what's the reason behind that development? Speaking from a user's perspective, if I may, that seems a bit too much :)


However, the onbeforeunload javascript event seems to be what you need.

Create a New script and every time the user tries to leave the app the browser's default confirmation message will pop up. Please note that the support for this varies depending on the browser.

Cristiana Umbelino wrote:

Just for curiosity sake - what's the reason behind that development? Speaking from a user's perspective, if I may, that seems a bit too much :)


However, the onbeforeunload javascript event seems to be what you need.

Create a New script and every time the user tries to leave the app the browser's default confirmation message will pop up. Please note that the support for this varies depending on the browser.

The reason is just business related (client wants the users to be warned that they will not complete the request if they leave the page).

The onbeforeunload (tryed that) works when trying to close the window but it doesn't allow me to show a custom popup and it doesn't cover the navigate (this is a reactive web app).



Cristiana Umbelino wrote:

Just for curiosity sake - what's the reason behind that development? Speaking from a user's perspective, if I may, that seems a bit too much :)


However, the onbeforeunload javascript event seems to be what you need.

Create a New script and every time the user tries to leave the app the browser's default confirmation message will pop up. Please note that the support for this varies depending on the browser.


We also used this in ours. The javascript calls a client action that does what we want when the user tries to close the browser.

Juan Carlos Elorde wrote:

Cristiana Umbelino wrote:

Just for curiosity sake - what's the reason behind that development? Speaking from a user's perspective, if I may, that seems a bit too much :)


However, the onbeforeunload javascript event seems to be what you need.

Create a New script and every time the user tries to leave the app the browser's default confirmation message will pop up. Please note that the support for this varies depending on the browser.


We also used this in ours. The javascript calls a client action that does what we want when the user tries to close the browser.

Hi Juan

Can you provide a sample of what you did?

Thanks


Sorry for the very late response. Note on this though, this is not my code. Of my team lead :)


function beforeClosing (e){
    e.preventDefault();
    e.returnValue = '';
}

window.addEventListener('beforeunload', beforeClosing);

window.addEventListener('unload', function(event) {
        window.removeEventListener('beforeunload', beforeClosing);
});


Basically, the important call here is the function beforeClosing. Then you are just calling it on the events of the window.