I found this general problem, but it seems that after googling there is no obvious solution.
On my page, some user action will trigger an AJAX request. With YUI 2.X, the code is as follows:
Connect.asyncRequest("POST", url,
{
'failure' : function() { alert('failed'); },
'success': function() { doSuccess(); },
'scope": this,
},
dataStr);
When the AJAX request fails, I would like to open a dialog (not necessarily warn) to indicate a failure. However, if the AJAX request takes a few seconds, and I click the Refresh button to reload the page, a dialog always appears. This is not good. Therefore, I want to cancel the dialog on reboot.
When the page reloads, the failure callback will be called using the {status: 0, statusText: "communication failure"} object. It does not distinguish itself from other connection failures. Thus, I cannot judge whether this unloading phase is based on a callback object.
My current workaround is to listen for the "beforeunload" event.
Event.on(window, 'beforeunload', function() { isUnloading = true; });
The failure callback checks whether to display a dialog based on the value of isUnloading.
This works, but the "beforeunload" event is not standard. Is there a better way to handle this?
thank