Skip to content

Instantly share code, notes, and snippets.

var f = (e, t) => {
return (5, 6) + 2;
}
var e = function () {
return f(8,8) + 4;
}
window['a b'] = function () {
return e(5, 7);
@weizman
weizman / onunload-debugger-example.js
Last active January 30, 2019 22:12
run this code in devtools console and than try to shutdown the tab - see how Chrome suddenly respects `debugger;` statement!
// run this code in devtools console and than try to shutdown
// the tab - see how Chrome suddenly respects `debugger;` statement!
window.onunload = (e) => {
debugger;
};
@weizman
weizman / unload-event-listener-actions-limitation-demo.js
Last active January 30, 2019 22:13
demonstration of what can be done inside an `unload`/`beforeunload` listener
window.addEventListener('unload', (e) => {
function sleep(delay) {
const start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
// unloading won't finish until 10 full seconds pass!
sleep(10000);
setTimeout(() => {
@weizman
weizman / onunload-properties-same-location-in-memory-demo.js
Last active January 30, 2019 22:17
demonstration of how onbeforeunload and onunload properties all point to the same place
function listener() {}
const frameset = document.createElement('frameset');
const body = document.body;
window.onunload = listener;
window.onbeforeunload = listener;
body.onunload === listener; // true
body.onbeforeunload === listener; // true
@weizman
weizman / unload-supporting-objects-demo.js
Last active January 30, 2019 22:18
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
@weizman
weizman / unload-priority-demo.js
Last active January 30, 2019 22:18
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) {
@weizman
weizman / unload-legit-actions-demo.js
Last active January 30, 2019 22:19
demonstrate how normal actions can be performed within an `unload` event listener except for breakpoints
window.addEventListener('unload', (e) => {
console.log('unloading!'); // would successfully log to console
localStorage.setItem('key', 'value'); // would successfully set new item to local storage
debugger; // would fail to break at this point!
});
@weizman
weizman / cancelable-beforeunload-event-demo.js
Last active January 30, 2019 22:19
cancelable `beforeunload` event example
// by https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload#Example
window.addEventListener("beforeunload", function (event) {
event.preventDefault(); // Cancel the event as stated by the standard.
event.returnValue = ''; // Chrome requires returnValue to be set.
});