Skip to content

Instantly share code, notes, and snippets.

@weizman
Last active January 30, 2019 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weizman/aaee40d105ca4f52911eade6e7c3337e to your computer and use it in GitHub Desktop.
Save weizman/aaee40d105ca4f52911eade6e7c3337e to your computer and use it in GitHub Desktop.
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');
document.body.appendChild(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