HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML oncanplay Attribute
Photo Credit to CodeToFun
🙋 Introduction
The oncanplay
attribute in HTML is an event handler attribute associated with audio and video elements.
It is used to specify a JavaScript function to be executed when media (audio or video) can start playing without having to stop for buffering.
🎯 Purpose of oncanplay
The primary purpose of the oncanplay
attribute is to provide a way to execute custom JavaScript code when the media is ready to play.
This event is triggered when the browser determines that there's enough media data buffered to start playback without interruptions.
💎 Values
The oncanplay
attribute accepts a JavaScript function as its value. This function will be executed when the canplay event occurs. Here's a basic example:
<audio controls oncanplay="myFunction()">
<source src="audio.mp3" type="audio/mp3">
Your browser does not support the audio tag.
</audio>
<script>
function myFunction() {
alert("Media can start playing!");
}
</script>
🧠 How it Works
In this example, the oncanplay
attribute is set to trigger the myFunction() JavaScript function when the media is ready to play.
🔄 Dynamic Values with JavaScript
You can dynamically set the oncanplay
attribute using JavaScript.
This can be useful when you want to change the behavior of the event handler based on specific conditions or user interactions. Here's an example:
<script>
// Dynamically set oncanplay for a media element
document.getElementById("myMedia").oncanplay = function() {
console.log("Media can start playing dynamically!");
};
</script>
🧠 How it Works
In this script, the oncanplay
attribute is set to an anonymous function for a media element with the id "myMedia." This function will be executed when the canplay event occurs.
🏆 Best Practices
- Use the
oncanplay
attribute to execute custom JavaScript code when media is ready to play. - Consider dynamic assignment of the attribute when you need to change the behavior based on specific conditions.
- Test your code across different browsers to ensure consistent behavior.
🎉 Conclusion
The oncanplay
attribute is a valuable tool for enhancing the functionality of audio and video elements in HTML.
By using this attribute, you can execute custom code precisely when the media is ready to play, providing a more interactive and responsive user experience.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML oncanplay Attribute), please comment here. I will help you immediately.