views:

25

answers:

0

I have this... well okay not really this but this is the important stuff:

window.onbeforeunload = function()
{
    return 'you sure?';
}

... and I have this inside my $.ajax() calls:

error: function(xhr, textStatus, errorThrown)
{
    if (textStatus == 'timeout')
    {
        HandleError('Connection error.');
        return;
    }
    if (xhr.status == 500)
    {
        HandleError('Server error.');
        return;
    }
    HandleError('Application error.');
}

HandleError() right now simply displays a stylized alert explaining what went wrong. 'Application error' is just a catchall for anything else that could have gone wrong; I could probably do better with this but it works for now.

When someone closes the window and accepts the beforeunload prompt, usually it's in the middle of an AJAX call, since they're happening roughly every three seconds. And while they see nothing wrong going on (the error alert never appears, because the window is gone), my Firebug reports that HandleError() doesn't exist. Basically, when they closed the window, it caused an AJAX error, which then tried to call HandleError(), but at that point the page had unloaded and it was gone.

How can I tell the error function to not care if the window is being closed, or is this just something I'll have to live with? I could set a 'don't throw error' variable when beforeunload is called, but there's be no way to know to unset it, right?