👀 Live Preview
Start loading an image, then cancel it to trigger abort:
Waiting…

The onabort attribute is an inline event handler that runs JavaScript when a media resource stops loading before it finishes. It applies to elements like <img>, <audio>, <video>, and <track>. The abort event fires when loading is canceled—for example when script clears src, assigns a new URL mid-load, or the user navigates away. It is not the same as a failed load: a broken image URL triggers onerror, not onabort.
Inline JS.
img audio video.
Not failed.
Abort vs fail.
Preferred way.
Property API.
onabort AttributeThe primary purpose of onabort is to respond when a media download is interrupted before completion. Single-page apps that swap image galleries may cancel in-flight requests. Lazy-loading libraries might abort loads for off-screen images. You can log canceled requests, release memory, or show a placeholder when a load does not finish.
The old reference incorrectly suggested that a missing image or “failed to load” triggers onabort. That is onerror. onabort means the browser stopped an in-progress fetch—not that the server returned 404 or the format was invalid.
Inline onabort="…" works for tutorials, but production code should use element.addEventListener("abort", handler) to separate HTML from JavaScript.
Set onabort to a JavaScript statement or function call on a media element:
<img
src="photo.jpg"
alt="Profile photo"
onabort="handleAbort()">
<video src="clip.mp4" onabort="logCanceled()"></video>
<script>
function handleAbort() {
console.log("Image load was canceled.");
}
</script>abort event fires.<img>, <audio>, <video>, and <track>.element.onabort = function() { … }.element.addEventListener("abort", handler).AbortController fetch API—related concept, different API.The onabort attribute accepts a string of JavaScript code:
onabort="handleAbort()" — Call a named function.onabort="console.log('canceled')" — Inline statement.onabort="this.alt='Load canceled'" — Use this for the element.img.onabort = function (e) { … } assigns the same handler.const img = document.getElementById("photo");
img.addEventListener("abort", () => {
console.log("Load canceled");
});
// Cancel in-progress load
img.src = "";| Event | When it fires | Handler |
|---|---|---|
abort | Load canceled before finish | onabort / addEventListener |
error | Load failed (404, network) | onerror |
load | Load completed successfully | onload |
| Cancel via JS | img.src = "" mid-load | May trigger abort |
| Broken URL | File not found | Triggers error, not abort |
| Element | Supported? | Notes |
|---|---|---|
<img> | Yes | Most common use |
<video>, <audio> | Yes | Media resource loading |
<track> | Yes | Subtitle/track file load |
<form>, <a> | No | Different event model |
onabort vs onerror vs onload| Handler | Typical cause | Example |
|---|---|---|
onabort | Load stopped before done | Script clears src |
onerror | Resource could not load | 404, corrupt file |
onload | Resource loaded OK | Image displayed |
Inline onabort handler, addEventListener, and abort vs error comparison.
Start loading an image, then cancel it to trigger abort:
Waiting…
Call a function when loading is aborted:
<img
src="photo.jpg"
alt="Example"
onabort="handleImageAbort()">
<script>
function handleImageAbort() {
console.log("Image loading was canceled.");
}
</script>When the browser aborts fetching the image resource, it runs the JavaScript in onabort. The handler runs once per canceled load attempt.
Attach the handler with addEventListener or onabort property:
<img id="dynamicImage" alt="Dynamic">
<script>
const img = document.getElementById("dynamicImage");
img.addEventListener("abort", () => {
alert("Image loading aborted!");
});
img.src = "photo.jpg";
img.src = ""; // cancel mid-load
</script>Assign the handler before or during loading. Clearing src or changing it while a fetch is active can fire abort on the previous request.
Do not use a broken URL to demo onabort—that fires error:
<!-- Cancel load → abort -->
img.addEventListener("abort", () => log("canceled"));
img.src = "photo.jpg";
img.src = "";
<!-- Bad URL → error -->
img.addEventListener("error", () => log("failed"));
img.src = "missing.jpg";abort, error, and load are separate events on media elements. Pick the handler that matches the situation you need to handle.
<img>, always set meaningful alt; abort handlers often swap to fallback content that must stay accessible.alert() in demos with inline status messages in real apps.Browser fetches img/video src.
src cleared or changed mid-fetch.
onabort or listener executes.
Log, placeholder, retry UI.
The abort event and onabort handler are supported in all modern browsers on <img>, <audio>, <video>, and <track>. Exact timing of when abort fires may vary slightly if the resource loads from cache instantly.
onabort supportAll major browsers fire the underlying event and honor the onabort handler attribute.
Bottom line: Use addEventListener("abort", …) in modern apps; test cancel behavior with slow or uncached resources.
onabort in production code.onerror for failed loads, onabort for canceled loads.src changes, not 404 URLs.The onabort attribute runs JavaScript when media loading is canceled before it completes. It applies to img, audio, video, and track elements.
Understand the difference from onerror, prefer addEventListener("abort", …), and use abort handlers to clean up or improve UX when loads are intentionally stopped.
onabortBookmark these before wiring your next event handler.
Not failed.
Eventimg video audio.
Scope404 = error.
ComparePreferred.
ModernTriggers abort.
Pattern<img>, <audio>, <video>, and <track>.onerror. onabort is for canceled in-progress loads.addEventListener("abort", handler) in modern code. Inline onabort is fine for learning and small demos.element.src = "", remove src, or assign a new URL while the previous load is still in progress.img.onabort = function() { … } or img.addEventListener("abort", handler).Practice inline onabort, addEventListener, and abort vs error in the Try It editor.
5 people found this page helpful