I don’t have an actual use case for this, but I’m curious, whether there is a way to react (callback), if a user clicks on “stay on this page” when window.onbeforeunload was triggered.
function warning(){
if(true){
console.log('leaving');
return "You are leaving the page";
}
}
window.onbeforeunload = warning;
There is no callback for staying on the page, but there is one for leaving the page,
window.unload.Try setting a timeout in
beforeunload, then clear it in unload. If you stay, the timeout will run, otherwise it’ll be cleared.DEMO: http://jsfiddle.net/aPwfz/1/