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/e3edde3ed61e06a41468efa31753073f to your computer and use it in GitHub Desktop.
Save weizman/e3edde3ed61e06a41468efa31753073f to your computer and use it in GitHub Desktop.
demonstration of how the priority of beforeunload and unload events works
const ifr = document.createElement('iframe');
document.body.appendChild(ifr);
// top window's beforeunload event would be the first one to fire
window.onbeforeunload = function(e) {
console.log('first event to fire (1)');
};
// other windows beforeunload events would fire after top window
ifr.contentWindow.onbeforeunload = function(e) {
console.log('second event to fire (2)');
};
// top window's unload event would fire after all beforeunload events have been fired
window.onunload = function(e) {
console.log('third event to fire (3)');
};
// other windows unload events would fire after top window
ifr.contentWindow.onunload = function(e) {
console.log('fourth event to fire (4)');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment