Skip to content

Instantly share code, notes, and snippets.

Last active January 30, 2019 22:18
What would you like to do?
demonstration of how the priority of beforeunload and unload events works
const ifr = document.createElement('iframe');
// 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