Sub topic 5
Fallback content for unsupported browsers
This chapter assumes you can write basic HTML documents, know how <audio> and <video> load a file with src, and have seen the <source> element. It covers what fallback content is, when a browser shows it, and what it cannot do.
What fallback content is
Anything you place between the opening and closing tags of <audio> or <video> that is not a <source> or <track> element is fallback content. A browser that implements media elements does not render it. A browser that does not implement them treats <video> as an unknown element and renders its children as ordinary HTML.
<audio controls src="podcast-episode.mp3">
<a href="podcast-episode.mp3">Download the episode</a>
</audio>A browser that supports <audio> shows its player and hides the link. An old browser shows only the link.
What triggers fallback, and what does not
Fallback content is shown when the browser has no support for the element. This is rare today, because every current browser supports both elements. The case that matters more often is different: the browser knows the element but cannot play the file.
| Situation | Is fallback content shown? | What the user sees |
|---|---|---|
| Browser does not know `<video>` | Yes | The fallback markup |
| Browser knows `<video>` but supports no listed format | No | A player with an error state, or an empty box |
| File returns 404 | No | A player that cannot load |
| JavaScript is disabled | No | The player still works if `controls` is set |
This distinction is the main gotcha. A "Your browser cannot play this video" message inside the element will not appear when the browser supports the element but not your codec. The message is correct only for browsers without media element support.
<video controls width="640" poster="poster.jpg" preload="metadata">
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
<track kind="captions" src="clip.en.vtt" srclang="en" label="English" default>
<p>Your browser cannot play this video. <a href="clip.mp4">Download it instead</a>.</p>
</video>In this example the <source> and <track> children are not fallback. Only the <p> is.
Writing useful fallback content
Good fallback content gives the user a way to reach the media or its information. Three common choices:
- A download link to the file, which works in any browser that can open or save the file.
- A short text description or transcript of the media.
- A pointer to a hosted version, such as a link to a page that hosts the video.
Keep the link target a format with wide support. In the example above the link goes to the MP4 file, which most media players can open.
Avoid placing a second <video> or a plugin <object> inside the fallback. Plugin-based players such as Flash are no longer supported in any mainstream browser, so those patterns from older tutorials do nothing today.
Always include a transcript path
A download link helps users with an unsupported browser. A transcript helps more people: users who are deaf or hard of hearing, users on slow connections, and users who prefer to read. You can put a transcript link in the fallback, or better, next to the player where everyone can see it, since fallback content is hidden in nearly every browser.
<audio controls src="podcast-episode.mp3">
<p>
<a href="podcast-episode.mp3">Download the episode</a>
or <a href="podcast-episode-transcript.html">read the transcript</a>.
</p>
</audio>
<p><a href="podcast-episode-transcript.html">Read the transcript</a></p>Handling playback failure with JavaScript
Because the browser does not show fallback content when a format is unsupported, you can detect failure yourself. Media elements fire an error event on the element, and on the last <source> child when each source fails. Listening on the element itself does not catch source failures, because error events from <source> elements do not bubble. Attach the listener to the last <source>, or use the capture phase on the element.
<video id="clip" controls width="640">
<source src="clip.webm" type="video/webm">
<source src="clip.mp4" type="video/mp4">
</video>
<p id="video-message" hidden>
This video could not be played.
<a href="clip.mp4">Download it instead</a>.
</p>
<script>
const video = document.querySelector('#clip');
const message = document.querySelector('#video-message');
video.addEventListener('error', () => {
message.hidden = false;
}, true);
</script>The true argument registers the listener for the capture phase. That lets the video element see error events fired by its <source> children before they would stop propagating. A source error does not always mean total failure, since the browser may move on to the next source. If you want to show the message only when everything failed, check video.currentSrc and video.networkState.
video.addEventListener('error', () => {
const noneLoaded = video.networkState === HTMLMediaElement.NETWORK_NO_SOURCE;
if (noneLoaded) {
message.hidden = false;
}
}, true);NETWORK_NO_SOURCE means the element found no usable source. For a single src attribute, video.error is set to a MediaError object, and its code property tells you whether the failure was a network problem, a decode problem, or an unsupported source.
Common mistakes
- Putting the only copy of important information in fallback content. Almost no one sees it, so place essential text outside the element.
- Writing "Update your browser" as the fallback. The real cause is more often a missing codec or a broken file, and the message is not shown in those cases.
- Using fallback links to a format the user's browser also cannot play. Link to the most widely supported file.
- Placing fallback content before
<source>elements. Browsers handle this correctly, but keeping<source>and<track>first makes the markup easier to read. - Forgetting
controls. Without it, the user sees no player and no way to play, even though the element is supported.
Exercises
Exercise
A page has a <video> with a single WebM <source> and a <p> fallback saying "Your browser cannot play this video." A user on a browser that supports <video> but not WebM opens the page. Do they see the message? What would you add so they can still reach the content?
Show answer
No. The browser supports the element, so it hides the fallback and shows a player in an error state. Add an MP4 <source> with type="video/mp4", a visible download link or transcript outside the element, and optionally an error listener in capture mode that reveals a message when networkState equals HTMLMediaElement.NETWORK_NO_SOURCE.
Exercise
Why does a plain video.addEventListener('error', handler) not fire when every <source> child fails, and how do you fix it?
Show answer
The error event fires on the <source> elements and does not bubble to the <video>. Register the listener with the capture flag (addEventListener('error', handler, true)) or attach it to the last <source> element.