HTML oncanplaythrough Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Media

Introduction

The oncanplaythrough attribute is an inline event handler that runs JavaScript when the canplaythrough event fires. That happens on <audio> and <video> when the browser estimates enough media is buffered to play through to the end without stopping to load more data. Use it to hide loading overlays, enable confident autoplay, or show “ready” status. It is stronger than oncanplay (which only means playback can start) but is still an estimate—not a guarantee on slow networks.

What You’ll Learn

01

Event handler

Inline JS.

02

canplaythrough

Media event.

03

Play through

Buffer estimate.

04

vs oncanplay

Start vs through.

05

addEventListener

Preferred way.

06

.oncanplaythrough

Property API.

Purpose of oncanplaythrough Attribute

The primary purpose of oncanplaythrough is to react when buffering looks sufficient for uninterrupted playback. You might remove a full-screen loader, enable a custom player, or start autoplay (where policy allows). It typically fires after canplay once more of the file has buffered.

The old reference mixed up titles (calling an example “onprogress”) and function names. Also remember: canplaythrough is the browser’s estimate, not a promise that buffering will never pause. Seeking or poor connectivity can still trigger waiting later.

💡
Prefer addEventListener on the media element

Production code should use video.addEventListener("canplaythrough", handler). Inline oncanplaythrough works for learning but separates concerns less cleanly.

📝 Syntax

Set oncanplaythrough on audio or video, or assign element.oncanplaythrough:

oncanplaythrough.html
<script>

  function handleCanPlayThrough() {

    console.log("Media can play through.");

  }

</script>



<video controls oncanplaythrough="handleCanPlayThrough()">

  <source src="clip.mp4" type="video/mp4">

</video>

Syntax Rules

  • Value is JavaScript code executed when the canplaythrough event fires.
  • Applies to <audio> and <video> media elements.
  • Fires when the browser estimates uninterrupted playback is possible.
  • JavaScript: media.oncanplaythrough = function() { … }.
  • Modern alternative: media.addEventListener("canplaythrough", handler).
  • Usually follows canplay in the media load sequence.

💎 Values

The oncanplaythrough attribute accepts a string of JavaScript code:

  • oncanplaythrough="handleCanPlayThrough()" — Call a named function.
  • oncanplaythrough="hideLoader()" — Inline statement.
  • JavaScript: video.oncanplaythrough = () => { … } assigns the handler.
oncanplaythrough-js.html
const video = document.getElementById("clip");

video.addEventListener("canplaythrough", () => {

  document.getElementById("loader").hidden = true;

});

video.addEventListener("canplay", () => {

  document.getElementById("playBtn").disabled = false;

});

⚡ Quick Reference

EventWhen it firesHandler
canplayEnough data to startoncanplay
canplaythroughCan play to end (estimate)oncanplaythrough
loadeddataFirst frame readyonloadeddata
Hide full loaderIn canplaythrough handlerCommon pattern
Enable play soonerUse oncanplay insteadLighter threshold

Applicable Elements

TargetSupported?Notes
<audio>YesInline oncanplaythrough on video
<video>YesMost common use
<source>NoEvent fires on parent media element
<input>, <img>NoNot media canplay events

oncanplay vs oncanplaythrough vs onloadeddata

HandlerWhen it firesTypical use
onloadeddataFirst frame of media availableShow poster / first frame
oncanplayEnough buffered to startEnable play button, hide loader
oncanplaythroughEnough buffered to play throughAutoplay long content confidently

Examples Gallery

Inline oncanplaythrough on video, addEventListener, and comparison with oncanplay.

👀 Live Preview

Load the video — when enough is buffered to play through, status updates:

Waiting for canplaythrough…

Example — Inline oncanplaythrough on video

Run a function when playback can continue without stopping:

inline-oncanplaythrough.html
<video controls oncanplaythrough="handleCanPlayThrough()">

  <source src="clip.mp4" type="video/mp4">

</video>



<script>

  function handleCanPlayThrough() {

    console.log("Media can play through.");

  }

</script>
Try It Yourself

How It Works

The browser fires canplaythrough on the video element when buffering looks sufficient. The inline oncanplaythrough attribute wires up the handler.

Dynamic Values with JavaScript

Attach the handler with addEventListener:

dynamic-oncanplaythrough.html
<script>

  document.getElementById("clip").addEventListener("canplaythrough", function () {

    console.log("Video can play through.");

  });



  // Or property form:

  document.getElementById("clip").oncanplaythrough = function () { /* … */ };

</script>
Try It Yourself

How It Works

Register the listener once at page load. When buffering looks sufficient, canplaythrough runs your readiness code.

Example — oncanplaythrough vs oncanplay

Listen for both events on the same element:

canplay-compare.html
audio.addEventListener("canplay", () => { /* can start */ });

audio.addEventListener("canplaythrough", () => { /* can play through */ });
Try It Yourself

How It Works

Use oncanplay to enable play early; use oncanplaythrough when you need confidence before autoplay or removing a full loader.

♿ Accessibility

  • Provide captions and transcripts — Media events do not replace accessible alternatives for audio and video content.
  • Do not autoplay with sound — Respect user preferences; use canplay to enable controls, not surprise playback.
  • Avoid alert() on canplay — Update visible status or hide loaders instead of blocking dialogs.
  • Keyboard controls — Native controls attribute supports keyboard users; custom players need full keyboard support.
  • Loading state — Announce buffer readiness to assistive tech with live regions if you hide native controls.

🧠 How oncanplaythrough Works

1

Media starts loading

Browser fetches the file.

loadstart
2

canplay may fire

Enough to start playback.

oncanplay
3

More data buffers

Estimate improves.

Buffering
=

canplaythrough fires

Play-through ready.

Browser Support

The canplaythrough event and oncanplaythrough handler are supported in all modern browsers on <audio> and <video> elements — Chrome, Firefox, Safari, and Edge.

DOM Events · Fully supported

Universal oncanplaythrough support

All major browsers fire the underlying event and honor the oncanplaythrough handler attribute.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
oncanplaythrough attribute 99% supported

Bottom line: Universal support on media elements. Test canplay vs canplaythrough timing with network throttling.

💡 Best Practices

✅ Do

  • Use addEventListener on the media element — Clearer than inline oncanplaythrough.
  • Hide full loaders on canplaythrough — Use oncanplay if you only need to enable play sooner.
  • Handle waiting events — Re-buffering can still happen after canplaythrough.
  • Provide fallbacks — Include text inside audio/video for unsupported browsers.
  • Test on slow networks — Throttle in DevTools to see event timing.

❌ Don’t

  • Do not confuse with canplay — canplaythrough is a higher buffer threshold.

Conclusion

The oncanplaythrough attribute runs JavaScript when the browser estimates enough media is buffered and the canplaythrough event fires on audio or video. Use it to hide full loaders and enable confident playback UI.

Prefer addEventListener("canplaythrough", …), understand it is stronger than oncanplay but still an estimate, and handle waiting if buffering pauses later.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about oncanplaythrough

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

audio / video

Media only.

Scope
🔄 03

Hide full loader

After canplaythrough.

Pattern
📝 04

addEventListener

Preferred.

Modern
05

Buffer estimate

Not a guarantee.

Note

❓ Frequently Asked Questions

It runs JavaScript when the canplaythrough event fires — when the browser estimates enough media is buffered to play through without stopping.
audio and video elements. The event fires on the media element loading the resource.
oncanplay means playback can start. oncanplaythrough means the browser estimates uninterrupted playback is possible.
No. It is the browser buffer estimate at that moment. Slow networks or seeking can still cause waiting later.
Prefer media.addEventListener("canplaythrough", …). Inline oncanplaythrough works for simple demos.
Usually after canplay, once more data has buffered. For short files they may fire close together.

Handle media play-through readiness

Practice inline oncanplaythrough, addEventListener, and comparison with oncanplay in the Try It editor.

Try oncanplaythrough demo →

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.

5 people found this page helpful