HTML oncanplay Event Attribute


The oncanplay HTML event attribute specifies a script to run when the browser can start playing the specified media (when it has buffered enough to begin). It is a non-cancelable event that does not bubble.

The oncanplay event is fired when the browser can start playing a media, but estimates that not enough data has been loaded to play the media up to its end without having to stop for further buffering of content.

This event is often used to start playing a media when it is ready, without having to wait for the entire media to be loaded. It can also be used to display a loading indicator while the media is buffering, and then hide the indicator when the media is ready to play.

Here is an example of how to use the oncanplay event attribute:

<video id="myVideo" oncanplay="myFunction()">
  <source src="myVideo.mp4" type="video/mp4">

function myFunction() {
  // Start playing the video

In this example, the myFunction() function will be called when the browser can start playing the video, but before the entire video has been loaded. This function can then start playing the video, or perform any other actions that are needed.

The oncanplay event is a useful event attribute for controlling the playback of media on your web pages.


<element oncanplay="script">


  • scriptThe name of the script to use when the event has been triggered.


<!DOCTYPE html>
<meta charset="utf-8">
<title>Событие oncanplay (элемент audio)</title>

<audio id="myAudio" controls oncanplay="myFunction()">
<source src="audio.mp3" type="audio/mpeg">
Your browser does not support the audio element.

function myFunction() {
alert("EXAMPLE: Audio file ready to play");

<p>Demonstration of the "oncanplay" attribute in the AUDIO element.</p>


Browser Support

The following table will show you the current browser support for the HTML oncanplay Event Attribute.

Edge Chrome Firefox Opera Safari
Tablets / Mobile
Chrome Firefox Opera Safari Samsung Webview

Last updated by CSSPortal on: 14th October 2023