Skip to content

Instantly share code, notes, and snippets.

Last active January 30, 2019 22:18
What would you like to do?
objects that support beforeunload and unload events
function successful_listener(e) {
console.log('would successfully fire!');
function failing_listener(e) {
console.log('would never fire - this message would never be logged to console');
// both ways to register both beforeunload and unload events would work with window
window.addEventListener('beforeunload', successful_listener);
window.addEventListener('unload', successful_listener);
window.onbeforeunload = successful_listener;
window.onunload = successful_listener;
// addEventListener won't work with document.body whereas direct registration would
document.body.addEventListener('beforeunload', failing_listener);
document.body.addEventListener('unload', failing_listener);
document.body.onbeforeunload = successful_listener;
document.body.onunload = successful_listener;
// addEventListener won't work with frameset whereas direct registration would
const frameset = document.createElement('frameset');
frameset.addEventListener('beforeunload', failing_listener);
frameset.addEventListener('unload', failing_listener);
frameset.onbeforeunload = successful_listener;
frameset.onunload = successful_listener;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment