HTML onloadstart Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

loadstart

Fetch begins.

03

audio / video

Media only.

04

Loading UI

Spinners.

05

addEventListener

Preferred way.

06

Event order

First in chain.

Purpose of onloadstart Attribute

The 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.

💡
Not for images

<img> uses onload when the image finishes loading. loadstart belongs to HTMLMediaElement — use it on audio and video.

📝 Syntax

Set onloadstart on an audio or video element:

onloadstart.html
<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>

Syntax Rules

  • Value is JavaScript executed when the loadstart event fires.
  • Works on <audio> and <video> only.
  • Fires when the browser starts looking for / fetching media data.
  • First event in the standard media loading timeline.
  • Pass this from inline handler or use event.target in listeners.
  • Modern alternative: media.addEventListener("loadstart", handler).
  • Also fires again after element.load() or when src changes.

💎 Values

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.
  • JavaScript: video.onloadstart = () => { … } or property assignment on the element.
onloadstart-js.html
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";
});

⚡ Quick Reference

Property / eventWhen it firesNotes
loadstartFetch beginsFirst media loading event
loadedmetadataAfter loadstartDuration known
loadeddataLaterFrame data ready
canplayLater stillCan start playback
Handler attributeonloadstartInline on media element

Applicable Elements

TargetSupported?Notes
<audio>YesStandard use case
<video>YesStandard use case
<img>NoUse onload instead
<body> / windowNoUse page onload
<source> aloneNoListen on parent media element

onloadstart vs onloadedmetadata vs onload

HandlerElementWhen it fires
onloadstartaudio / videoMedia fetch begins
onloadedmetadataaudio / videoDuration / dimensions known
onloadbody, img, iframeResource or page finished loading

Examples Gallery

Show a loading message, attach with addEventListener, and pair loadstart with canplay.

👀 Live Preview

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

Status: idle

Example — Inline onloadstart handler

Show feedback when media fetch begins:

audio-onloadstart.html
<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>
Try It Yourself

How It Works

loadstart fires before metadata or frame data. Pass this to read currentSrc.

Dynamic Values with JavaScript

Attach with addEventListener:

dynamic-onloadstart.html
<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>
Try It Yourself

How It Works

Register in script for cleaner separation. Use a named element reference instead of relying on this.

Example — Pair loadstart with canplay

Show loading on start, ready when playback is possible:

loadstart-canplay.html
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";
});
Try It Yourself

How It Works

loadstart opens the loading state; canplay closes it when enough data is buffered.

♿ Accessibility

  • Announce loading state — Use visible text or aria-live="polite" on a status region.
  • Do not block controls — Keep native controls usable unless you provide full keyboard-accessible replacements.
  • Pair with error handling — Use onerror so failed loads are not stuck on “Loading…” forever.
  • Reduce motion — Respect prefers-reduced-motion for spinner animations.
  • Captions for video — Add <track kind="captions"> for accessible video content.

🧠 How onloadstart Works

1

src set or load()

Browser starts fetch.

Request
2

loadstart fires

onloadstart runs.

Event
3

Later events

metadata, data, canplay.

Timeline
=

Loading UI

User sees progress.

Browser Support

The loadstart event and onloadstart handler are supported wherever HTML5 audio and video are supported.

Media Events · Fully supported

Universal onloadstart support

All major browsers fire loadstart on audio and video elements.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onloadstart attribute 99% supported

Bottom line: Reliable for loading indicators on audio/video in all modern browsers.

💡 Best Practices

✅ Do

  • Use on audio and video for loading UX
  • Pair with canplay or loadeddata to hide spinners
  • Use addEventListener("loadstart", …) in production
  • Handle onerror when fetch fails
  • Reset UI on onemptied when media is cleared

❌ Don’t

  • Put onloadstart on <img> — it does not apply
  • Confuse with page body onload
  • Use alert() for loading feedback
  • Leave “Loading…” visible if load fails
  • Attach handlers only to <source> — use parent media element

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onloadstart

Bookmark these before building media players.

5
Core concepts
🎧 02

Media only

audio / video.

Scope
🔄 03

Loading UI

Spinners.

UX
04

Not img

Use onload.

Gotcha
05

Then canplay

Hide spinner.

Pair

❓ Frequently Asked Questions

It runs JavaScript when the loadstart event fires — when the browser begins fetching media data for audio or video.
No. Images use onload when loading completes. loadstart is for HTML media elements.
loadstart = fetch begins. loadedmetadata = duration and dimensions are known.
When you change src, call element.load(), or switch sources — each new load triggers loadstart again.
addEventListener("loadstart", handler) is preferred for production code.
Yes in all modern browsers with HTML5 media; Internet Explorer 9+.

Master media loading events

Practice the onloadstart attribute with loading status examples in the Try It editor.

Try onloadstart demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful