Hi, I'm trying to submit a form depending on the result of a var, and specifically it is a window confirmation to save the form if the user is leaving the page. This is the code I tried:
Code:
var savebeforeexit = window.confirm("Are you sure?");
if (savebeforeexit) {
document.myform.submit();
} else {
//do nothing
}
If it is from window.onbeforeunload it will never work.
And quite frankly, window.onbeforeunload is the only reliable place to put a "now leaving the page" trap. But the most you can do there is allow the user to cancel his request to leave the page.
(I have wondered if you could make a synchronous HTTP request at that point. I think you could, but that's beginning to get pretty complex.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
var savebeforeexit = window.confirm("Are you sure?");
confirm() gives your visitor three alternative responses in most browsers - in some browsers the third alternative is to disable JavaScript completely for the web page at that point in which case the call would never return to assign a value to savebeforeexit.
Calls to alert() confirm() and prompt() should only ever be used for debugging, they should never be used in a live web page.