HTML | DOM oncanplay Event
The HTML DOM oncanplay event occurs when a specified audio/video is buffered enough to begin.
The order of events occur During the loading process of an audio/video:
- onloadstart
- ondurationchange
- onloadedmetadata
- onloadeddata
- onprogress
- oncanplay
- oncanplaythrough
Syntax:
In HTML:
<element oncanplay="myScript">
In JavaScript:
object.oncanplay = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("canplay", myScript);
Example: Using HTML
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green"> GeeksforGeeks </h1> <h2>HTML DOM oncanplay event</h2> <video controls oncanplay="myFunction()"> <source src="Geekfun.mp4" type="video/mp4"> </video> <script> function myFunction() { alert("Can start playing video"); } </script> </center> </body> </html> |
Output:


Example: Using JavaScript
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green"> GeeksforGeeks </h1> <h2>HTML DOM oncanplay event</h2> <video controls id="myVideo"> <source src="Geekfun.mp4" type="video/mp4"> </video> <script> document.getElementById("myVideo").oncanplay = function() { myFunction() }; function myFunction() { alert("Can start playing video"); } </script> </center> </body> </html> |
Output:


Example: In JavaScript, using the addEventListener() method:
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green"> GeeksforGeeks </h1> <h2>HTML DOM oncanplay event</h2> <video controls id="myVideo"> <source src="Geekfun.mp4" type="video/mp4"> </video> <script> document.getElementById( "myVideo").addEventListener( "canplay", myFunction); function myFunction() { alert("Can start playing video"); } </script> </center> </body> </html> |
Output:


Supported Browsers: The browsers supported by DOM oncanplay Event are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Apple Safari
- Opera
Recommended Posts:
- HTML | DOM touchend Event
- HTML | DOM onloadedmetadata Event
- HTML | DOM oncanplaythrough Event
- HTML | DOM onchange Event
- HTML | DOM onstalled Event
- HTML | DOM onkeypress Event
- HTML | DOM onkeydown Event
- HTML | DOM onblur Event
- HTML | DOM onkeyup Event
- HTML | DOM onloadeddata Event
- HTML | DOM onload Event
- HTML | DOM touchcancel Event
- HTML | DOM ontoggle Event
- HTML | DOM ondragenter Event
- HTML | DOM ondragend Event
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



