Created
June 1, 2015 09:43
-
-
Save Yogatopia/b207c671f289ecbddb65 to your computer and use it in GitHub Desktop.
Confirmation on Leaving the Current Page in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function goodbye(e) { | |
if(!e) e = window.event; | |
//e.cancelBubble is supported by IE - this will kill the bubbling process. | |
e.cancelBubble = true; | |
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog | |
//e.stopPropagation works in Firefox. | |
if (e.stopPropagation) { | |
e.stopPropagation(); | |
e.preventDefault(); | |
} | |
} | |
window.onbeforeunload=goodbye; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onbeforeunload = function (e) { | |
var message = "Your confirmation message goes here.", | |
e = e || window.event; | |
// For IE and Firefox | |
if (e) { | |
e.returnValue = message; | |
} | |
// For Safari | |
return message; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onbeforeunload=function(e){ | |
// only take action (iterate) if my SCHEDULED_REQUEST object contains data | |
for (var key in SCHEDULED_REQUEST){ | |
postRequest(SCHEDULED_REQUEST); // post and empty SCHEDULED_REQUEST object | |
for (var i=0;i<1000;i++){ | |
// do something unnoticable but time consuming like writing a lot to console | |
console.log('buying some time to finish saving data'); | |
}; | |
break; | |
}; | |
}; // no return string --> user will leave as normal but data is send to server |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment