Is there a way to capture whether the user clicked OK or CANCEL?
I need to do something ONLY if the user is leaving the page....
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.
Here is the solution I am using in a situation where I need to do something (such as clear the session) ONLY when the user navigates from the page.
I have 2 global vars
var clearSession = true;
var confirmExit = true;
window.onbeforeunload = function() { return confirmExit(); }
window.onunload = function() { return clearSession(); }
function confirmExit() {
if (needToConfirm == true) {
return "exit page?";
}
}
function clearSession() {
if (clearSession == true) {
alert("Killing the session on the server!!!");
PageMethods.ClearSession();
}
}
Then of course, on every page submit/button/drop down list etc...you need to make sure that the above global variables are set to false.
Hope this helps someone.
No, browsers allow you to only specify the text in the alert box, but no way to catch the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With