👀 Live Preview
Status updates when loading starts (reload the page or change source to see it again):
Status: idle

The onloadstart attribute is an inline event handler for the loadstart event on media elements — <audio> and <video>. It runs when the browser begins fetching media data — the first event in the media loading timeline. Use it to show a loading spinner, reset progress UI, or log that buffering has started. It is not the same as body onload (page ready) or img onload (image finished). Images do not fire loadstart in the HTML media event model.
Inline JS.
Fetch begins.
Media only.
Spinners.
Preferred way.
First in chain.
onloadstart AttributeThe primary purpose of onloadstart is to react at the very start of media loading — before metadata, frame data, or playback readiness. That is the right moment to display “Loading…”, disable a play button, or reset a progress bar when the user picks a new track or you change src.
In the loading sequence, loadstart comes first, then progress, loadedmetadata, loadeddata, and canplay. Pair onloadstart with onloadeddata or oncanplay to hide the spinner when enough data arrives.
<img> uses onload when the image finishes loading. loadstart belongs to HTMLMediaElement — use it on audio and video.
Set onloadstart on an audio or video element:
<audio controls onloadstart="showLoading(this)">
<source src="track.mp3" type="audio/mpeg">
</audio>
<video controls onloadstart="handleLoadStart(this)">
<source src="clip.mp4" type="video/mp4">
</video>loadstart event fires.<audio> and <video> only.this from inline handler or use event.target in listeners.media.addEventListener("loadstart", handler).element.load() or when src changes.The onloadstart attribute accepts a string of JavaScript code:
onloadstart="showLoading(this)" — Pass the media element.onloadstart="handleLoadStart(event)" — Use event.target inside the function.video.onloadstart = () => { … } or property assignment on the element.player.addEventListener("loadstart", () => {
document.getElementById("status").textContent = "Loading media…";
document.getElementById("status").className = "loading";
});
player.addEventListener("canplay", () => {
document.getElementById("status").textContent = "Ready to play";
document.getElementById("status").className = "ready";
});| Property / event | When it fires | Notes |
|---|---|---|
loadstart | Fetch begins | First media loading event |
loadedmetadata | After loadstart | Duration known |
loadeddata | Later | Frame data ready |
canplay | Later still | Can start playback |
| Handler attribute | onloadstart | Inline on media element |
| Target | Supported? | Notes |
|---|---|---|
<audio> | Yes | Standard use case |
<video> | Yes | Standard use case |
<img> | No | Use onload instead |
<body> / window | No | Use page onload |
<source> alone | No | Listen on parent media element |
onloadstart vs onloadedmetadata vs onload| Handler | Element | When it fires |
|---|---|---|
onloadstart | audio / video | Media fetch begins |
onloadedmetadata | audio / video | Duration / dimensions known |
onload | body, img, iframe | Resource or page finished loading |
Show a loading message, attach with addEventListener, and pair loadstart with canplay.
Status updates when loading starts (reload the page or change source to see it again):
Status: idle
Show feedback when media fetch begins:
<audio controls onloadstart="handleLoadStart(this)">
<source src="track.mp3" type="audio/mpeg">
</audio>
<p id="status"></p>
<script>
function handleLoadStart(media) {
document.getElementById("status").textContent =
"Loading: " + media.currentSrc;
}
</script>loadstart fires before metadata or frame data. Pass this to read currentSrc.
Attach with addEventListener:
<audio id="myAudio" controls>
<source src="track.mp3" type="audio/mpeg">
</audio>
<script>
document.getElementById("myAudio").addEventListener("loadstart", function () {
document.getElementById("log").textContent = "Media loading has started";
});
</script>Register in script for cleaner separation. Use a named element reference instead of relying on this.
Show loading on start, ready when playback is possible:
const player = document.getElementById("player");
const status = document.getElementById("status");
player.addEventListener("loadstart", () => {
status.textContent = "Loading…";
status.className = "loading";
});
player.addEventListener("canplay", () => {
status.textContent = "Ready to play";
status.className = "ready";
});loadstart opens the loading state; canplay closes it when enough data is buffered.
aria-live="polite" on a status region.onerror so failed loads are not stuck on “Loading…” forever.prefers-reduced-motion for spinner animations.<track kind="captions"> for accessible video content.Browser starts fetch.
onloadstart runs.
metadata, data, canplay.
User sees progress.
The loadstart event and onloadstart handler are supported wherever HTML5 audio and video are supported.
onloadstart supportAll major browsers fire loadstart on audio and video elements.
Bottom line: Reliable for loading indicators on audio/video in all modern browsers.
audio and video for loading UXcanplay or loadeddata to hide spinnersaddEventListener("loadstart", …) in productiononerror when fetch failsonemptied when media is clearedonloadstart on <img> — it does not applybody onloadalert() for loading feedback<source> — use parent media elementThe onloadstart attribute runs JavaScript when audio or video loading begins — the first hook in the media timeline for spinners, status text, and reset logic.
Use it on media elements only, prefer addEventListener, and follow with canplay or loadeddata when loading finishes.
onloadstartBookmark these before building media players.
Fetch begins.
Timelineaudio / video.
ScopeSpinners.
UXUse onload.
GotchaHide spinner.
Pairloadstart event fires — when the browser begins fetching media data for audio or video.onload when loading completes. loadstart is for HTML media elements.loadstart = fetch begins. loadedmetadata = duration and dimensions are known.src, call element.load(), or switch sources — each new load triggers loadstart again.addEventListener("loadstart", handler) is preferred for production code.Practice the onloadstart attribute with loading status examples in the Try It editor.
5 people found this page helpful