Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

oncanplaythrough — handling estimated media readiness

The oncanplaythrough attribute contains JavaScript that handles the canplaythrough event. For audio and video, this signals an estimated ability to play through to the end without further buffering, not a guarantee of uninterrupted playback.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<audio oncanplaythrough="/* JavaScript */"></audio>
<video oncanplaythrough="/* JavaScript */"></video>

oncanplaythrough is an event handler content attribute for canplaythrough. Its value is JavaScript code, not a Boolean value. Typical uses involve audio and video elements; the handler code can access the event object.

The browser queues canplaythrough when readyState changes to HAVE_ENOUGH_DATA (4). This usually means it estimates that playback at the current playbackRate can reach the end without further buffering. That state also covers situations where waiting longer cannot obtain more data. It is neither a guarantee nor confirmation that the whole file has downloaded. canplay indicates a weaker level of readiness: the ability to start or resume playback.

Do not confuse the attribute name oncanplaythrough with the event name canplaythrough. Alternatives are assigning a function to the element.oncanplaythrough property and using element.addEventListener('canplaythrough', handler). The attribute contains code, whereas the property receives a function.

The example deliberately dispatches a synthetic event so verification requires neither a media file nor a network connection. The expected paragraph text is canplaythrough; this tests handler wiring only, not actual media readiness.

Examples

<!doctype html>
<meta charset="utf-8">
<title>oncanplaythrough test</title>
<audio id="media" oncanplaythrough="document.getElementById('status').textContent = event.type"></audio>
<p id="status">Pending</p>
<script>
  const media = document.getElementById('media');
  media.dispatchEvent(new Event('canplaythrough'));
</script>

Result

canplaythrough

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources