CKTIS / HTML
onunhandledrejection — unhandled promise rejection
The onunhandledrejection attribute on body registers a handler for the Window object's unhandledrejection event. It exposes the rejection reason of a promise that still lacks rejection handling when notification occurs.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<body onunhandledrejection="/* JavaScript using event */">onunhandledrejection is an event handler attribute whose value is JavaScript code. On body, it sets the handler for the Window object's unhandledrejection event, not a separate element event. It is not an ordinary global HTML attribute available on every element. Its JavaScript counterpart is the window.onunhandledrejection property.
Notification is asynchronous. Attaching rejection handling early enough prevents the event. The event object is a PromiseRejectionEvent: event.promise identifies the rejected promise, and event.reason contains the rejection reason.
Calling event.preventDefault() cancels default console reporting for this event; it does not repair the promise. Handle expected failures at their source rather than relying solely on global notification.
The example sets the attribute on body and handles the promise rejection with promise.catch(() => {}). The call to window.dispatchEvent() dispatches a synthetic, cancelable unhandledrejection event on window, invoking the handler that logs event.reason. This demonstrates that the handler is wired up, not that the browser notifies it of an unhandled rejection. The call to event.preventDefault() here applies only to the synthetic event and does not demonstrate suppression of the browser's default reporting for an unhandled rejection. Inline code requires permission under the page's security policy.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Body onunhandledrejection handler</title>
</head>
<body onunhandledrejection="event.preventDefault(); console.log('reason: ' + event.reason)">
<script>
const reason = 'demo';
const promise = Promise.reject(reason);
// Handle the fixture so the browser does not report a real unhandled rejection.
promise.catch(() => {});
window.dispatchEvent(new PromiseRejectionEvent('unhandledrejection', {
promise,
reason,
cancelable: true
}));
</script>
</body>
</html>Result
reason: demo
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.