Onbeforeunload - Can I get a reason for the call? I want to identify the redirect

I need a 'verify save' pop-up message using the onbeforeunload event (when the user closes the window or clicks the link that “leaves” the Angular application page).

This works fine, but I realized that when I get a 302 redirect, the event also fires.
Now I understand that I am before the page is unloaded, but I do not want it to start when the page is redirected.

How to detect event redirection?

$window.onbeforeunload = function (event) {
       // how can I identify if this event was invoked due to a redirect?    
       return "If you leave the page now your changes won't be saved.";
}
+4
source share

Source: https://habr.com/ru/post/1539766/


All Articles