onunhandledrejection
A promise rejection has no handler at the reporting point. Window event handler, exposed through the body element in HTML; register on window rather than on an arbitrary element. Event interface: PromiseRejectionEvent. For the described native event: bubbles: no; cancelable: yes. Use for diagnostics, not a replacement for handling promises; canceling suppresses default reporting, and some cross-origin rejections are not exposed. Example: Observe an existing rejected promise from a standalone test page; this example deliberately does not create an unhandled rejection. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.
Formal Syntax & Structure
<body onunhandledrejection="handleEvent(event)"> window.addEventListener("unhandledrejection", event => { /* handle event */ });Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| onunhandledrejection | JavaScript handler body | Optional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute. |
| event | PromiseRejectionEvent | Event object delivered to the addEventListener callback; event.type is "unhandledrejection". Inspect target, currentTarget, bubbles and cancelable as appropriate. |
Working Example
<p>Window event monitor</p><p role="status" id="log">Waiting for unhandledrejection</p><script> const target = window; const log = document.getElementById("log"); target.addEventListener("unhandledrejection", event => { log.textContent = String(String(event.reason)); });</script>