Well, I said this is a decision, not , a .
( AJAX), Stay on this page, , javaScript ( return).
jQuery.ajax() async: true, , , setTimeout() ( , )
var stayHere = "";
window.onbeforeunload = userConfirmation;
function userConfirmation() {
var confirmation = "Your edits will be lost";
stayHere = setTimeout(function() {
$.ajax({
url: "ajax-call.php",
type: "post",
data: {chosenOpt: "stay"},
async: true,
success: function(data) { console.log(data);},
error: function() { alert("error");}
});
}, 2000);
return confirmation;
}
, Leave this page
, , unload, :
jQuery(window).on("unload", function() {
clearTimeout(stayHere);
$.ajax({
url: "ajax-call.php",
type: "post",
data: { chosenOpt: "leave"},
async: false,
success: function(data) { console.log(data);},
error: function() { console.log("Error");}
});
});
. , unload. unload , , .
jsfiddle, , .
ajax-call.php
echo $_POST["chosenOpt"] . "_" . rand(1, 9999);
leave_[number] Leave this page stay_[number] Stay on this page
. leave_[number] , .