Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onUnload() only fires once for confirm() dialoge

Status
Not open for further replies.

Azathoth

Technical User
Jul 14, 2003
61
US
I have a confirm dialogue bound to the onUnload() handler asking the user if s/he wants to leave the current page. If cancel is pressed, the document stops on the current page, as it is supposed to. However, if after this happens, the user clicks on a link to another page again, the dialogue box doesn't execute. It's as if the property of the page that records the loaded/unloaded status is stuck in unloaded, even though the current page is still there. Here's the code:

form_dirty = true;
window.onunload = warn_form_data_loss;

function warn_form_data_loss ()
{
if ( form_dirty == true )
{
var answer = ( confirm( "WARNING: You are about to navigate away from the current page, which may result in data loss. Would you like to proceed?" ) );
if (answer)
{
}
else
{
document.execCommand('Stop');
}
}
}

Normally this wouldn't be too much of a problem, but never underestimate the stupidity of you user ;)...Any ideas?
 
First I'll just point out some things about syntax. When checking a boolean variable for true/false status, using the == operator is not necessary. Also, when checking a variable for false status you can use the ! operator. To clean up your code a bit you could do this:
Code:
function warn_form_data_loss () {
    if (form_dirty) {
        var answer = ( confirm( "WARNING: You are about to navigate away from the current page, which may result in data loss.  Would you like to proceed?" ) );
        if (!answer) {
            document.execCommand('Stop');
        }
    }
}

Now, to address your question. I'm not sure how simulating the stop command modifies the integrity of the page, so I won't rule out that as a possibility. But, the way you describe your problem suggests that the confirm portion of the function is not even executed, which would suggest that form_dirty is false when the function is called the 2nd time. Is this a possibility? Since you have posted nothing that shows the form_dirty value being modified, it's hard to say that it's the cause, but that's where I'd start first.

-kaht

banghead.gif
 
No, form_dirty has nothing to do with it - I didn't even realize I included that in the post. That's for when the submit button is pressed so the dialogue isn't called...I haven't worried about that yet.

The confirm is definately not executed the second time, even if I return false to the onunload() event handler. If you return false to an onclick() handler bound to a link, it stops the page from loading. Apparently the behavior between these two handlers is different, because onunload() definately requires a manual stop.

Anyways, I suppose if there's no real solution, it's not a big deal...just slightly bothersome.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top