HTML oncanplaythrough Attribute

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

Definition and Usage

The oncanplaythrough attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the canplaythrough event fires — the browser estimates enough data is buffered to play to the end without stopping for more buffering.

That is stronger than oncanplay (enough to start). It is still an estimate, not a hard guarantee — network changes can still cause buffering later.

Baseline · Widely available.

Not deprecated and not experimental. The canplaythrough event is stable across modern browsers since about July 2015. It does not bubble and is not cancelable.

Prefer addEventListener in apps.

Inline oncanplaythrough is fine for learning. Production code usually uses media.addEventListener("canplaythrough", handler) so logic stays in script and multiple listeners are easy.

HTML Syntax

syntax.html
<video|audio oncanplaythrough=" script">

script is JavaScript that runs when canplaythrough fires. Equivalent JS: media.oncanplaythrough = handler or media.addEventListener("canplaythrough", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when the browser estimates play-through is possibleTry it
oncanplaythrough="fn()"Call a named function from the attributeTry it
addEventListener("canplaythrough")Preferred modern listener on the media elementTry it
media.oncanplaythrough = …Assign one handler via the IDL property (replaces a previous assignment)Try it
With canplayUsually fires after canplay; stronger readiness signalTry it

Live Preview

Load a sample clip. When the browser estimates it can play through, status updates. Use Reload to fire canplaythrough again.

Media

Sample media from /video/count.mp4 or /audio/count.mp3. Short local files often fire quickly.

Player

Waiting for canplaythrough…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline oncanplaythrough on Audio

Call a function when the browser estimates the track can play through.

HTML
<audio controls oncanplaythrough="handleCanPlayThrough()">
  <source src="/audio/count.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
<p id="status">Waiting for canplaythrough…</p>

<script>
  function handleCanPlayThrough() {
    document.getElementById("status").textContent =
      "canplaythrough fired — estimated play-through OK.";
  }
</script>
Try It Yourself

How It Works

  1. Put oncanplaythrough on the media element — here <audio>, not on <source>.
  2. Browser buffers until it estimates uninterrupted play-through (readyState HAVE_ENOUGH_DATA).
  3. canplaythrough fires and the attribute runs handleCanPlayThrough().
  4. Update UI (status, enable a confident play button) — avoid alert() in real apps.

Example 2 — addEventListener on Video

Same event with the preferred script pattern.

HTML
<video id="clip" width="400" height="225" controls>
  <source src="/video/count.mp4" type="video/mp4">
  Your browser does not support the video element.
</video>
<p id="log">Loading media…</p>

<script>
  document.getElementById("clip").addEventListener("canplaythrough", function () {
    document.getElementById("log").textContent =
      "canplaythrough: estimated play to end without stopping.";
  });
</script>
Try It Yourself

How It Works

  1. Select the video with getElementById.
  2. Register canplaythrough with addEventListener (keeps HTML clean).
  3. When the estimate says play-through is OK, the listener updates the status line.
  4. You can add more listeners later — unlike a single .oncanplaythrough = … assignment.

Example 3 — canplay vs canplaythrough

Log both events so you can see start-ready vs play-through-ready.

HTML
<audio id="audio" controls>
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<ul id="log"></ul>

<script>
  var audio = document.getElementById("audio");
  var log = document.getElementById("log");

  function add(line) {
    var li = document.createElement("li");
    li.textContent = line;
    log.appendChild(li);
  }

  audio.addEventListener("canplay", function () {
    add("canplay — enough to start");
  });
  audio.addEventListener("canplaythrough", function () {
    add("canplaythrough — estimated play through");
  });
</script>
Try It Yourself

How It Works

  1. canplay — enough buffered to begin (HAVE_FUTURE_DATA).
  2. canplaythrough — browser estimates it can reach the end without stopping (HAVE_ENOUGH_DATA).
  3. Order — canplay usually comes first; on short local files both may fire almost together.
  4. Pick the signal you need — enable play early with canplay; wait for a stronger buffer with canplaythrough.

Edge Cases & Pitfalls

Most surprises come from treating the event as a guarantee, or waiting forever on sparse preload.

estimate

Not a hard guarantee

The browser estimates download rate and buffer. Conditions can worsen and you may still see waiting.

vs start

Stronger than canplay

canplay means “can start.” canplaythrough means “estimated play to end.” Use the right one for your UI.

preload

May wait on preload="none"

With little preloading, enough data for play-through may not load until the user presses play. See preload.

target

Listen on audio/video, not source

The event targets the media element. Nested <source> tags do not receive oncanplaythrough.

property

.oncanplaythrough = replaces listeners

Assigning the IDL property overwrites a previous assignment. Prefer addEventListener when you need more than one handler.

Browser Support

oncanplaythrough is an HTML event-handler attribute for media elements in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

oncanplaythrough

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the canplaythrough event on audio and video.

97% Global support (approx.)
Google Chrome 3+
Full support
Mozilla Firefox 3.5+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 10.5+
Full support
Samsung Internet Modern · Full
Full support
Internet Explorer 9+
Supported
oncanplaythrough Widely supported

Bottom line: Safe to use for stronger media-ready UI. Prefer addEventListener in production, and treat the signal as an estimate — not a promise.

Key Takeaways

  • Role: run script when the browser estimates media can play through without stopping.
  • Vs start: use oncanplay for “can begin”; this attribute for “estimated play to end.”
  • Elements: <audio> and <video> only.
  • Apps: prefer addEventListener("canplaythrough", …).

One line: oncanplaythrough means “estimated play to the end” — stronger than canplay, still not a promise.

Frequently Asked Questions

It is an event-handler attribute on audio or video. When the browser estimates enough data is buffered to play to the end without stopping, the canplaythrough event fires and the JavaScript in oncanplaythrough runs.
The HTML audio and video elements. Put the handler on the media element itself, not on nested source tags.
No. It is the browser’s estimate based on buffered data and download rate. Network conditions can still change and cause buffering later.
oncanplay means enough data to start (readyState HAVE_FUTURE_DATA). oncanplaythrough means the browser estimates it can play to the end without further buffering (HAVE_ENOUGH_DATA).
Both work. For learning, oncanplaythrough in HTML is fine. In production code, prefer media.addEventListener("canplaythrough", handler) so you can attach multiple listeners and keep script out of markup.
No. The canplaythrough event and oncanplaythrough handler are standard HTML media features and Baseline widely available since about July 2015.

Next: onchange

Run JavaScript when a form control’s value changes and is committed.

onchange 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