Arguments not being passed to window.onerror function

Viewed 144

I have an error logging function along with a call to an undefined variable t:

const errortext = document.getElementById("errortext");

window.addEventListener("error", function (msg, file_loc, line_no) {
  errortext.innerHTML += msg + " " + file_loc + " " + line_no;
});

t

It fires, but errortext is this:

[object ErrorEvent] undefined undefined

My question is, why arguments are not being passed, and what can I do to fix it?

1 Answers

If you refer to the MDN docs on global error handlers here it says

For historical reasons, different arguments are passed to window.onerror and element.onerror handlers (as well as on error-type window.addEventListener handlers).

Under addEventListener it shows the signature

window.addEventListener('error', function(event) { ... })

while it looks like onerror gives you the params you are expecting

window.onerror = function(message, source, lineno, colno, error) { ... };
Related