Reputation: 1800
In JavaScript when beforeunload event is executed and the warning box pops up, is it possible to get information what button was clicked?
Upvotes: 0
Views: 176
Reputation: 1442
You can't do this directly unfortunately as onbeforeunload
asks the browser to create the dialog and doesn't tell you the return value but you can use a global variable which you set in the event and check it via setInterval
for example.
var confirmUnload = false;
window.addEventListener("beforeunload", function(event) {
confirmUnload = true;
event.returnValue = "Really leave?";
});
window.addEventListener("unload", function(event) {
console.log("User left");
});
setInterval(function(){
if(confirmUnload) {
confirmUnload = false;
setTimeout(function() {
console.log('Still here');
}, 500);
}
}, 400);
Upvotes: 1