HTML oncanplay Attribute

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

Definition and Usage

The oncanplay attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the canplay event fires — the browser has enough buffered data that playback can start.

It does not mean the whole file is downloaded. Playback may still pause later to buffer. For an estimate that media can play through without stopping, use oncanplaythrough.

Baseline · Widely available.

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

Prefer addEventListener in apps.

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

HTML Syntax

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

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

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when enough data is buffered to start playbackTry it
oncanplay="fn()"Call a named function from the attributeTry it
addEventListener("canplay")Preferred modern listener on the media elementTry it
media.oncanplay = …Assign one handler via the IDL property (replaces a previous assignment)Try it
With other media eventsOften after loadedmetadata / loadeddata; before canplaythroughTry it

Live Preview

Load a sample clip. When the browser can start playback, status updates. Use Reload to fire canplay again.

Media

Sample media from /video/count.mp4 or /audio/count.mp3.

Player

Waiting for canplay…

HTML Examples

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

Example 1 — Inline oncanplay on Audio

Call a function when enough audio is buffered to start.

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

<script>
  function handleCanPlay() {
    document.getElementById("status").textContent =
      "canplay fired — enough data to start playback.";
  }
</script>
Try It Yourself

How It Works

  1. Put oncanplay on the media element — here <audio>, not on <source>.
  2. Browser buffers enough data that playback can begin (readyState reaches HAVE_FUTURE_DATA).
  3. canplay fires and the attribute runs handleCanPlay().
  4. Update UI (status text, hide a loader) — 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("canplay", function () {
    document.getElementById("log").textContent =
      "canplay event: ready to start playback.";
  });
</script>
Try It Yourself

How It Works

  1. Select the video with getElementById.
  2. Register canplay with addEventListener (keeps HTML clean).
  3. When buffering allows a start, the listener updates the status line.
  4. You can add more listeners later — unlike a single .oncanplay = … assignment.

Example 3 — Where canplay Fits in the Load Order

Log related media events so you can see the usual sequence.

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("loadedmetadata", function () {
    add("loadedmetadata — duration known");
  });
  audio.addEventListener("loadeddata", function () {
    add("loadeddata — first frame ready");
  });
  audio.addEventListener("canplay", function () {
    add("canplay — enough buffered to start");
  });
</script>
Try It Yourself

How It Works

  1. loadedmetadata — duration and dimensions (or track info) are known.
  2. loadeddata — first frame of media data is available.
  3. canplay — enough ahead that playback can start (may still buffer later).
  4. Next step often canplaythrough when the browser estimates it can play to the end.

Edge Cases & Pitfalls

Most surprises come from confusing “can start” with “can play through,” or expecting the event only once.

meaning

Start ≠ play through

canplay only means playback can begin. Slow networks can still hit waiting later. Use canplaythrough for a through-play estimate.

repeat

May fire more than once

Seeking or re-buffering can fire canplay again. Design handlers to be safe if they run repeatedly.

target

Listen on audio/video, not source

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

preload

Depends on buffering policy

With preload="none", little data may load until the user presses play — so canplay can be delayed.

property

.oncanplay = replaces listeners

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

Browser Support

oncanplay 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

oncanplay

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the canplay 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
oncanplay Widely supported

Bottom line: Safe to use for media readiness UI. Prefer addEventListener in production, and remember canplay is not the same as canplaythrough.

Key Takeaways

  • Role: run script when audio/video can start playing (canplay).
  • Not through: use oncanplaythrough for a play-to-end estimate.
  • Elements: <audio> and <video> only.
  • Apps: prefer addEventListener("canplay", …).

One line: oncanplay means “ready to start” — not “guaranteed to finish without buffering.”

Frequently Asked Questions

It is an event-handler attribute on audio or video. When the browser has buffered enough media that playback can start, the canplay event fires and the JavaScript in oncanplay runs.
The HTML audio and video elements. Put the handler on the media element itself, not on nested source tags.
No. It means playback can begin. The browser may still pause later to buffer. For an estimate that it can play through without stopping, use oncanplaythrough.
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, oncanplay in HTML is fine. In production code, prefer media.addEventListener("canplay", handler) so you can attach multiple listeners and keep script out of markup.
No. The canplay event and oncanplay handler are standard HTML media features and Baseline widely available since about July 2015.

Next: oncanplaythrough

React when the browser estimates media can play to the end without buffering.

oncanplaythrough 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