I'm trying to call an ajax before user leaving a page, this what i have done so far. But it doesn't even hit the ajax page. This is what i have done so far.
window.onbeforeunload = closeIt();
function closeIt()
{
var key="save-draft";
$.ajax({
url: "app/ajax_handler.php",
type:"GET",
data:{key:key},
success: function(data) {
return data;
}
});
}
I Have tried this one also both failed in my case.
$( window ).unload(function() {});
The only way I think is to let the user know that it's a process on background with a confirm message, that will block the exit until user click on Accept or you've got the response.
Something like that:
window.onbeforeunload = closeIt(); function closeIt() { /*var key="save-draft"; $.ajax({ url: "app/ajax_handler.php", type:"GET", data:{key:key}, success: function(data) { return data; } });*/ setTimeout(function() { return confirm("There is a process that isn't finished yet, you will lose some data. Are you sure you want to exit?"); }, 1000); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.