Possible Duplicate:
Capturing result of window.onbeforeunload confirmation dialog
I want to make user confirm page navigation like that:
window.onbeforeunload = function(e) {
return "Note that any unsaved data will be lost";
};
however, I want to detect if user clicked “Stay in this page” to do something important.
So is there anyway to detect which button is clicked?
NB: I tried adding confirm in the onbeforeunload function but chrome block it.
There is no way to execute code right after the user decided to stay on the page.
You can use the
onunloadevent to do something in case the user does not stay. If you want to send an AJAX request that’s one of the few cases whereasync: falseis appropriate.