The onerror attribute is an event-handler attribute. It runs JavaScript when the error event fires — a resource failed to load or cannot be used (for example a missing image, a blocked script file, or media the browser cannot decode).
Not deprecated and not experimental. Per MDN, element error is not cancelable and does not bubble. Stable since about July 2015.
Discouraged as inline HTML.
MDN discourages HTML event handler attributes. Prefer el.addEventListener("error", handler) in production; use inline onerror for quick demos and learning.
Syntax
HTML Syntax
syntax.html
<img|script|… onerror="script">
script is JavaScript that runs on load failure. Equivalent JS: el.onerror = handler or el.addEventListener("error", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Remove or clear the handler before swapping src (avoids a loop).
Load the fallback — if that succeeds, load fires instead of another error.
Example 3 — Compact One-liner Fallback
Clear the handler and set src in one attribute value.
HTML
<img src="/images/__broken__.webp" alt="Thumbnail"
onerror="this.onerror=null;this.src='/images/codetofun-logo.png';this.alt='Placeholder'">
<p>If the first URL fails, the placeholder appears.</p>
this.onerror = null disables further error callbacks on this element.
this.src = '…' requests the fallback file.
Keep it short — for larger logic, use a named function or addEventListener.
Edge Cases & Pitfalls
Most “page froze” bugs are an onerror that keeps swapping to another bad URL.
loop
Infinite fallback loop
If the fallback src also fails, error fires again. Set onerror = null (or remove the listener) before changing src.
abort
Not the same as abort
Canceled loads use onabort. Failed loads use error.
window
Not window.onerror
Element onerror is for resource load failures. window.onerror targets uncaught script errors and has a special multi-argument signature on Window.
api
Not cancelable
You cannot stop element error with preventDefault(). Recover in the handler (fallback UI, logging).
a11y
Update alt after swap
When you change src to a placeholder, set a clear alt so assistive tech describes the image the user actually sees.
Compatibility
Browser Support
onerror is an HTML event-handler attribute for resource elements in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onerror
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support error on img, script, and media.
97%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer9+
Supported
onerrorWidely supported
Bottom line: Safe to use for image/script/media failure UX. Prefer addEventListener('error') in apps, and always clear the handler before swapping src.
Remember
Key Takeaways
Role: resource failed to load or cannot be used.
Not cancelable and does not bubble.
Clear the handler before changing src to avoid loops.
Notonabort and not window.onerror.
One line:onerror means the resource failed — clear the handler, then show a fallback.
Frequently Asked Questions
It is an event-handler attribute. When a resource fails to load or cannot be used (broken image URL, missing script, media decode/network failure), the error event fires and the JavaScript in onerror runs.
Elements that load external resources — commonly img, script (with src), audio, video, link, and track. Put the handler on the element that owns the resource URL.
No. Per MDN, the element error event is not cancelable and does not bubble. You react after it fires (for example swap img.src).
onerror means the load failed. onabort means the load was canceled before it finished. They are different events.
No. Element onerror handles resource load failures on that element. window.onerror is a special Window handler for uncaught script errors (different signature and behavior).
No. The error event and onerror handler are standard and Baseline widely available since about July 2015. Inline HTML handlers are discouraged in production in favor of addEventListener.