I need to do remind the users something when they are leaving the page, and that can be handled with the window.onUnload event. But I also need to check if the user is navigating away by submitting the form on the page, or by clicking the navigation links. I could use the form's onSubmit event to set a flag, and then check against that flag in the window.onUnload event, but I am not sure which one fires first.
any ideas ?
You actually want window.onbeforeunload
It turns out that the form.onSubmit event fires first so i can use a flag. I have checked this in Firefox and Safari only.
Based on How to capture the browser window close event?. I added the refresh logic.