Reputation: 22015
I want to detect cases where an Ajax call is in progress and then it is interrupted due to some user action (e.g. user navigating to another page)?
I can see that when this happens my onreadystatechange handler gets called with readyState == 4 (DONE) and status == 0.
The question is, is there any way to be sure that this really corresponds to the described scenario (request aborted due to user action)? I want to avoid masking other -potentially more dangerous- errors.
Upvotes: 3
Views: 971
Reputation: 23264
For the user navigating to another page you could use the onbeforeunload
event.
For example:
window.onbeforeunload = function() {
// Check ajax request state and do something accordingly...
}
Upvotes: 3
Reputation: 609
You could set a variable (boolean) when the user fires the ajax request. Set it back in a callback function. If the user leaves the page, you could check this variable.
Upvotes: 1