Specification:
- Show alert when user leaves pages.
- Perform action when user confirms "Leave this page" (send ajax request and wait until response is received).
- When ajax response is received then leave page.
My code is:
window.onbeforeunload = function (e) {
e = e || window.event;
if (e) {
e.returnValue = 'Sure?';
}
$.ajax({
url: '/api/Electronic/leave',
dataType: "json",
contentType: "application/json",
cache: false,
type: 'POST',
data: myData,
success: function (data) {
//how to leave this page?
},
error: function () {
console.log("Error");
}
});
return 'Sure?';
};