HTML onabort Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onabort attribute is a global event handler for the abort event. On <audio> and <video>, that event fires when media loading stops before it finishes — not as a load error.

Typical trigger: clear src (or set it to "") and call load() while a fetch is still in progress. Prefer addEventListener("abort", …) in modern code.

Baseline · Widely available.

The media abort event is stable across modern browsers since about July 2015. Not deprecated.

Inline handlers discouraged.

MDN: HTML event handler attributes are discouraged. Study onabort to recognize it; use addEventListener in new projects.

HTML Syntax

syntax.html
<video onabort=" script">

script is JavaScript that runs when abort fires. Same idea on <audio>. Preferred: element.addEventListener("abort", handler).

Attribute Values

What you put in the attribute, and how it relates to listening:

Value / formDescriptionTry it
JavaScript statementInline handler body, e.g. logAbort()Try it
addEventListener("abort")Preferred modern listener (same event)Try it
vs onerrorAbort ≠ failed load; use error for failuresTry it

Live Preview

Start loading a video, then abort by clearing src and calling load(). Watch the status line.

action

Uses /video/count.mp4. Abort only fires if loading was still in progress when src was cleared.

Demo

Ready. Click Load & abort.

HTML Examples

Short study snippets. Use Try It Yourself for a live editor. Prefer listeners in real apps.

Example 1 — Inline onabort on Video

Recognize the attribute form. Clearing src mid-load can fire abort.

HTML
<video
  id="clip"
  width="280"
  controls
  onabort="document.getElementById('status').textContent = 'Loading aborted.'">
</video>
<p id="status">Idle</p>
<button type="button" id="go">Load & abort</button>

<script>
  var video = document.getElementById("clip");
  document.getElementById("go").addEventListener("click", function () {
    document.getElementById("status").textContent = "Loading…";
    video.src = "/video/count.mp4?t=" + Date.now();
    video.load();
    setTimeout(function () {
      video.removeAttribute("src");
      video.load();
    }, 50);
  });
</script>
Try It Yourself

How It Works

  1. onabort registers an inline handler for the media abort event.
  2. Start load by setting src and calling load().
  3. Abort by removing src and calling load() again quickly.
  4. Status text updates when the event fires (if load was still pending).

Example 2 — Preferred addEventListener

Same event, cleaner separation of HTML and script.

HTML
<video id="clip" width="280" controls></video>
<p id="status">Idle</p>
<button type="button" id="go">Load & abort</button>

<script>
  var video = document.getElementById("clip");
  var status = document.getElementById("status");

  video.addEventListener("abort", function () {
    status.textContent = "Loading aborted.";
  });

  document.getElementById("go").addEventListener("click", function () {
    status.textContent = "Loading…";
    video.src = "/video/count.mp4?t=" + Date.now();
    video.load();
    setTimeout(function () {
      video.removeAttribute("src");
      video.load();
    }, 50);
  });
</script>
Try It Yourself

How It Works

  1. No onabort attribute — the listener is attached in script.
  2. Same abort signal as the inline form.
  3. Easier to maintain — one place for handlers, no markup/script mix.
  4. IDL option: video.onabort = handler also works.

Example 3 — abort vs error

Abort is a canceled load; error is a failed load.

HTML
<video id="clip" width="280" controls></video>
<p id="status">Idle</p>
<button type="button" id="abortBtn">Abort load</button>
<button type="button" id="failBtn">Force error</button>

<script>
  var video = document.getElementById("clip");
  var status = document.getElementById("status");

  video.addEventListener("abort", function () {
    status.textContent = "abort — load stopped early (not an error).";
  });
  video.addEventListener("error", function () {
    status.textContent = "error — resource failed to load.";
  });

  document.getElementById("abortBtn").addEventListener("click", function () {
    status.textContent = "Loading…";
    video.src = "/video/count.mp4?t=" + Date.now();
    video.load();
    setTimeout(function () {
      video.removeAttribute("src");
      video.load();
    }, 50);
  });

  document.getElementById("failBtn").addEventListener("click", function () {
    status.textContent = "Loading bad URL…";
    video.src = "/video/does-not-exist-" + Date.now() + ".mp4";
    video.load();
  });
</script>
Try It Yourself

How It Works

  1. Abort path clears a valid src mid-load.
  2. Error path points at a missing file.
  3. Different events — do not treat abort as a failure UI.
  4. See also onerror for failure handling.

Edge Cases & Pitfalls

Most confusion comes from expecting abort on every element or mixing it up with error.

media

Useful on audio / video

The attribute is global in the HTML list, but browsers fire the media abort event on HTMLMediaElement. Do not expect it on random divs.

timing

May not fire if already loaded

If the resource finished (or never started) before you clear src, you might not see abort. Cache-bust the URL when testing.

bubble

Does not bubble

Listen on the media element itself. Parent listeners will not see a bubbling abort.

style

Prefer listeners

Inline on* attributes mix concerns and are harder to debug. Use addEventListener in production code.

vs error

Failed URL → error

A missing media file triggers error, not abort. Handle both if you show status to users.

Browser Support

onabort is an HTML event handler attribute for the abort event (mainly media). Prefer addEventListener in apps.

✓ Baseline · Widely available

onabort

Media abort is widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support abort on audio/video.

96% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer Legacy*
Partial*
abort Widely supported on media

Bottom line: *Safe on modern browsers for media abort. Prefer addEventListener over inline onabort.

Key Takeaways

  • Role: handler for media load stopped early (not an error).
  • Elements: mainly <audio> / <video>.
  • Prefer: addEventListener("abort", …) over inline attributes.
  • Not onerror: failed loads use the error event.

One line: onabort reacts when media loading is canceled mid-way — listen with abort, not error.

Frequently Asked Questions

It is an inline event handler for the abort event. On media elements, abort fires when resource loading stops before it finishes, but not because of a load error — for example when script clears src and calls load().
Browsers fire the media abort event on audio and video (HTMLMediaElement). The onabort attribute is listed as a global event handler, but it is mainly useful on those media elements.
abort means loading was stopped early without treating it as a failure. onerror means the resource failed to load (bad URL, network error, unsupported format).
Prefer element.addEventListener("abort", handler). MDN discourages inline HTML event handler attributes because they mix markup and script and are harder to maintain.
No. The media abort event is not cancelable and does not bubble.
Start loading a video or audio src, then quickly remove the src attribute (or set it to "") and call load(). If loading was still in progress, abort fires.

Next: onafterprint

Run script after the print dialog closes.

onafterprint tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful