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.
Syntax
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).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Value / pattern
Description
Try it
script
Any JavaScript run when the browser estimates play-through is possible
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…
Code
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>
Put oncanplaythrough on the media element — here <audio>, not on <source>.
Browser buffers until it estimates uninterrupted play-through (readyStateHAVE_ENOUGH_DATA).
canplaythrough fires and the attribute runs handleCanPlayThrough().
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>
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.