Created
March 1, 2022 23:35
-
-
Save DavidWells/18ff6633ce356237ef91c764812ee08a to your computer and use it in GitHub Desktop.
Attach multiple onError or onLoad listeners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fork of https://gist.github.com/alexreardon/8833460 | |
function addWindowEvent(event, fn) { | |
const existing = window[event] | |
if (typeof existing !== 'function') return fn | |
return function () { | |
existing.apply(window, arguments) | |
fn.apply(window, arguments) | |
} | |
} | |
const onError = addWindowEvent.bind(null, 'onerror') | |
const onLoad = addWindowEvent.bind(null, 'onload') | |
window.onerror = onError((msg) => { | |
console.log('msg', msg) | |
}) | |
window.onerror = onError((msg) => { | |
console.log('msg2', msg) | |
}) | |
window.onerror = onError((msg) => { | |
console.log('msg3', msg) | |
}) | |
setTimeout(function() { | |
throw new Error('test') | |
}, 3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment