Can anybody pls tell me, how can i make a AJAX request and send user to some other page when he performs following actions ?
- Clicks on next, prev btns of browser.
- refresh page by any way(f5,ctrl+r,refresh btn).
- try to close browser’s window(Here i don’t want to redirect him, just AJAX request).
i thought window.onbeforeunload could make it,
window.onbeforeunload = function () {
makeAjaxCall();
------
------
}
// Ajax Call is successful
function makeAjaxCall_SuccessHandler() {
window.location.href = 'Home.html';
}
but it is not working at all……..
after searching on this topic i found that this is a kind of rule made for browser’s security.
so pls tell me how to do this or any alternate way to achieve this.
You’re not describing the actual behavior you’re getting so I’m kinda guessing here, but I think your problem here is with the Asynchronous part of AJAX.
You see, when your onbeforeunload handler executes it may very well initiate the AJAX call BUT it won’t block waiting for its results to return. Instead the execution will continue and onbeforeunload will complete before you ever get any result from your AJAX request (if any call is made at all).
First of all add some debug traces to validate your handler is actually executed. If it is, add an idle loop to keep it busy (preventing it from returning) and see if the AJAX call comes through (I’d check the server side for that).
If that’s indeed the case I guess you’ll have to modify the idle loop to consult some kind of flag. Or even better, if the libraries you use allow it, make the server call synchronous.