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 onloadedmetadata Attribute
Photo Credit to CodeToFun
🙋 Introduction
The onloadedmetadata
attribute is an event handler in HTML that allows developers to execute JavaScript code when the metadata of a media element (like a video) has been loaded.
This event occurs once the initial metadata, such as duration and dimensions, has been retrieved, providing a trigger point for custom actions.
🎯 Purpose of onloadedmetadata
The primary purpose of the onloadedmetadata
attribute is to enable developers to perform actions or initiate scripts once the metadata of a media element is ready.
This can be useful for dynamically adapting the user interface or triggering other events based on the properties of the loaded media.
💎 Values
The onloadedmetadata
attribute is an event handler, and as such, it doesn't have multiple values like some other attributes.
It is assigned a JavaScript function that will be executed when the loadedmetadata event occurs.
📄 Example
Let's look at a simple example of how to use the onloadedmetadata
attribute in an HTML video element:
<video controls onloadedmetadata="handleMetadataLoaded()">
<source src="example.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<script>
function handleMetadataLoaded() {
console.log("Metadata loaded! Video duration:", this.duration);
// Add your custom actions here
}
</script>
🧠 How it Works
In this example, the onloadedmetadata
attribute is set to call the handleMetadataLoaded JavaScript function when the metadata of the video is loaded.
The function logs the video duration to the console, and you can customize it to perform any additional actions.
🔄 Dynamic Values with JavaScript
Similar to other event attributes, you can dynamically set the onloadedmetadata
attribute using JavaScript.
This allows for more flexibility, especially when the function to be executed depends on certain conditions or user interactions:
<script>
// Dynamically set onloadedmetadata for a video element
document.getElementById("dynamicVideo").onloadedmetadata = function() {
console.log("Dynamic metadata loaded!");
// Add your dynamic actions here
};
</script>
🧠 How it Works
In this script, the onloadedmetadata
attribute is dynamically set for a video element with the id dynamicVideo, and it logs a message to the console when the metadata is loaded.
🏆 Best Practices
- Use the
onloadedmetadata
attribute when you need to perform actions or set up functionality based on the loaded metadata of a media element. - Be cautious about performance implications, especially when handling large media files. Executing complex scripts on the loadedmetadata event may affect the responsiveness of your page.
- Always test your implementation across different browsers to ensure consistent behavior.
🎉 Conclusion
The onloadedmetadata
attribute provides a valuable tool for handling the loaded metadata event of media elements in HTML.
By leveraging this attribute, developers can create more dynamic and responsive applications that adapt to the properties of the loaded media.
👨💻 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 onloadedmetadata Attribute), please comment here. I will help you immediately.