CKTIS / JavaScript
window.onerror — global script error handling
window.onerror handles error events reported to the window. For an ErrorEvent it receives five arguments, and returning true cancels default error reporting without resuming the interrupted code.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
window.onerror = (message, source, lineno, colno, error) => { /* return true to cancel default reporting */ };
window.onerror = null;window.onerror is the Window event-handler property for error, used to observe uncaught script errors. It belongs to the HTML standard, not a particular ECMAScript edition. Contemporary browser engines support it; having Node 24 in a repository does not establish support for this browser API.
For an ErrorEvent, the function receives message (error message), source (script URL), lineno (line), colno (column), and error (the reported value, not necessarily an Error object). Details may be unavailable: muted cross-origin script errors have the message Script error., an empty URL, coordinates of 0, and null for error.
Returning exactly true cancels default reporting; false and undefined do not. This neither repairs the error nor resumes interrupted execution. window.addEventListener("error", handler) receives one event object; cancellation uses event.preventDefault(), not the return value.
Assignment replaces the previous handler, and null removes it. Unhandled promise rejections use unhandledrejection, not window.onerror. The example dispatches a synthetic ErrorEvent with fixed data and restores the previous handler; it does not produce a real uncaught exception.
Examples
(() => {
const previous = window.onerror;
const failure = new Error("Demo");
window.onerror = (message, source, lineno, colno, error) => {
console.log(`${message}|${source}|${lineno}|${colno}|${error === failure}`);
return true;
};
try {
const event = new ErrorEvent("error", {
message: "Demo",
filename: "demo.js",
lineno: 7,
colno: 3,
error: failure,
cancelable: true
});
window.dispatchEvent(event);
console.log(`canceled=${event.defaultPrevented}`);
} finally {
window.onerror = previous;
}
})();Result
Demo|demo.js|7|3|true canceled=true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.