Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created March 1, 2022 23:35
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 DavidWells/18ff6633ce356237ef91c764812ee08a to your computer and use it in GitHub Desktop.
Save DavidWells/18ff6633ce356237ef91c764812ee08a to your computer and use it in GitHub Desktop.
Attach multiple onError or onLoad listeners
// 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