Skip to content

Commit 8db892b

Browse files
authored
Add loading attribute for audio and video (mdn#43434)
1 parent 51872f3 commit 8db892b

File tree

14 files changed

+163
-19
lines changed

14 files changed

+163
-19
lines changed

files/en-us/learn_web_development/extensions/performance/best_practices/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Below is a quick review of best practices, tools, APIs with links to provide mor
2121
- Use a CDN for resources which can reduce load times significantly.
2222
- Compress your resources using [gzip](https://www.gnu.org/software/gzip/), [Brotli](https://github.com/google/brotli), and [Zopfli](https://github.com/google/zopfli).
2323
- Image optimization (use CSS animation, or SVG if possible).
24-
- Lazy loading parts of your application outside the viewport. If you do, have a backup plan for SEO (e.g., render full page for bot traffic); for example, by using the [`loading`](/en-US/docs/Web/HTML/Reference/Elements/img#loading) attribute on the {{HTMLElement("img")}} element
24+
- Lazy loading parts of your application outside the viewport. If you do, have a backup plan for SEO (e.g., render full page for bot traffic); for example, by using the [`loading`](/en-US/docs/Web/HTML/Reference/Elements/img#loading) attribute on the {{HTMLElement("img")}} element, or similarly on {{HTMLElement("iframe")}}, {{HTMLElement("video")}}, {{HTMLElement("audio")}} elements.
2525
- It is also crucial to realize what is really important to your users. It might not be absolute timing, but [user perception](/en-US/docs/Learn_web_development/Extensions/Performance/Perceived_performance).
2626

2727
## Quick Wins

files/en-us/learn_web_development/extensions/performance/html/index.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ Lazy loading has historically been handled using JavaScript, but browsers now ha
141141

142142
See [Browser-level image lazy loading for the web](https://web.dev/articles/browser-level-image-lazy-loading) on web.dev for detailed information.
143143

144-
You can also lazy load video content by using the `preload` attribute. For example:
144+
### Lazy loading video and audio
145+
146+
You can also lazy load video content until the video is played, by using the `preload` attribute. For example:
145147

146148
```html
147149
<video controls preload="none" poster="poster.jpg">
@@ -152,6 +154,26 @@ You can also lazy load video content by using the `preload` attribute. For examp
152154

153155
Giving `preload` a value of `none` tells the browser to not preload any of the video data before the user decides to play it, which is obviously good for performance. Instead, it will just show the image indicated by the `poster` attribute. Different browsers have different default video loading behavior, so it is good to be explicit.
154156

157+
Giving `preload` a value of `metadata` asks the browser to download the minimal data needed to display the video before playing (for example the length, dimensions, and maybe initial frame).
158+
159+
The `loading` attribute can further enhance lazy loading for videos by deferring loading of any video data, regardless of the `preload` value, as well as deferring loading of the `poster` image, until the video is near the viewport (at which point the `preload` value is used as per usual).
160+
161+
```html
162+
<video controls preload="none" poster="poster.jpg" loading="lazy">
163+
<source src="video.webm" type="video/webm" />
164+
<source src="video.mp4" type="video/mp4" />
165+
</video>
166+
```
167+
168+
This also can be used with audio content:
169+
170+
```html
171+
<audio
172+
controls
173+
src="/shared-assets/audio/t-rex-roar.mp3"
174+
loading="lazy"></audio>
175+
```
176+
155177
See [Fast playback with audio and video preload](https://web.dev/articles/fast-playback-with-preload) on web.dev for detailed information.
156178

157179
## Handling embedded content

files/en-us/learn_web_development/extensions/performance/multimedia/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Despite being the largest consumer of bandwidth, the impact of image downloading
5050

5151
### Loading strategy
5252

53-
One of the biggest improvements to most websites is [lazy-loading](/en-US/docs/Web/Performance/Guides/Lazy_loading) images beneath the fold, rather than downloading them all on initial page load regardless of whether a visitor scrolls to see them or not. Browsers provide this natively via the [`loading="lazy"`](/en-US/docs/Web/HTML/Reference/Elements/img#loading) attribute on the `<img>` element, and there are also many client-side JavaScript libraries that can do this.
53+
One of the biggest improvements to most websites is [lazy-loading](/en-US/docs/Web/Performance/Guides/Lazy_loading) images beneath the fold, rather than downloading them all on initial page load regardless of whether a visitor scrolls to see them or not. Browsers provide this natively via the [`loading="lazy"`](/en-US/docs/Web/HTML/Reference/Elements/img#loading) attribute on the `<img>` `<iframe>`, `<video>`, and `<audio>` elements, and there are also many client-side JavaScript libraries that can do this.
5454

5555
Beyond loading a subset of images, you should look into the format of the images themselves:
5656

files/en-us/learn_web_development/extensions/performance/perceived_performance/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Here are a few tips and tricks to help improve perceived performance:
6868

6969
To improve perceived performance, minimize the original page load. In other words, download the content the user is going to interact with immediately first, and download the rest after "in the background". The total amount of content downloaded may actually increase, but the user only _waits_ on a very small amount, so the download feels faster.
7070

71-
Separate interactive functionality from content, and load text, styles, and images visible at initial load. Delay, or lazy load, images or scripts that aren't used or visible on the initial page load. Additionally, you should optimize the assets you do load. Images and video should be served in the most optimal format, compressed, and in the correct size.
71+
Separate interactive functionality from content, and load text, styles, and images visible at initial load. Delay, or lazy load, images, iframes, media or scripts that aren't used or visible on the initial page load. Additionally, you should optimize the assets you do load. Images and video should be served in the most optimal format, compressed, and in the correct size.
7272

7373
### Prevent jumping content and other reflows
7474

files/en-us/web/api/htmliframeelement/loading/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ browser-compat: api.HTMLIFrameElement.loading
88

99
{{APIRef("HTML DOM")}}
1010

11-
The **`loading`** property of the {{domxref("HTMLIFrameElement")}} interface is a string that provides a hint to the {{Glossary("user agent")}} indicating whether the [iframe](/en-US/docs/Web/HTML/Reference/Elements/iframe) should be loaded immediately on page load, or only when it is needed.
11+
The **`loading`** property of the {{domxref("HTMLIFrameElement")}} interface is a string that provides a hint to the browser indicating whether the [iframe](/en-US/docs/Web/HTML/Reference/Elements/iframe) should be loaded immediately on page load, or only when it is needed.
1212

1313
This can be used to optimize the loading of the document's contents.
1414
Iframes that are visible when the page loads can be downloaded immediately (eagerly), while iframes that are likely to be offscreen on initial page load can be downloaded lazily — just before they will appear in the window's {{Glossary("visual viewport")}}.
@@ -41,6 +41,8 @@ All eagerly loaded iframes in the document must be fetched before the `load` eve
4141

4242
## Examples
4343

44+
### Basic usage
45+
4446
The example below shows how you might define a lazy-loaded iframe and then append it to a `<div>` in the document.
4547
The frame would then only be loaded when the frame about to become visible.
4648

files/en-us/web/api/htmlimageelement/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ _Inherits properties from its parent, {{domxref("HTMLElement")}}._
3939
- {{domxref("HTMLImageElement.isMap")}}
4040
- : A boolean value that reflects the [`ismap`](/en-US/docs/Web/HTML/Reference/Elements/img#ismap) HTML attribute, indicating that the image is part of a server-side image map. This is different from a client-side image map, specified using an `<img>` element and a corresponding {{HTMLElement("map")}} which contains {{HTMLElement("area")}} elements indicating the clickable areas in the image. The image _must_ be contained within an {{HTMLElement("a")}} element; see the `ismap` page for details.
4141
- {{domxref("HTMLImageElement.loading")}}
42-
- : A string providing a hint to the browser used to optimize loading the document by determining whether to load the image immediately (`eager`) or on an as-needed basis (`lazy`).
42+
- : A string indicating whether the browser should load the image immediately (`eager`) or when it is needed (`lazy`).
4343
- {{domxref("HTMLImageElement.naturalHeight")}} {{ReadOnlyInline}}
4444
- : Returns an integer value representing the intrinsic height of the image in CSS pixels, if it is available; else, it shows `0`. This is the height the image would be if it were rendered at its natural full size.
4545
- {{domxref("HTMLImageElement.naturalWidth")}} {{ReadOnlyInline}}

files/en-us/web/api/htmlimageelement/loading/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ browser-compat: api.HTMLImageElement.loading
88

99
{{APIRef("HTML DOM")}}
1010

11-
The **`loading`** property of the {{domxref("HTMLImageElement")}} interface provides a hint to the {{Glossary("user agent")}} on how to handle the loading of the image which is currently outside the window's {{Glossary("visual viewport")}}. This helps to optimize the loading of the document's contents by postponing loading the image until it's expected to be needed, rather than immediately during the initial page load. It reflects the `<img>` element's [`loading`](/en-US/docs/Web/HTML/Reference/Elements/img#loading) content attribute.
11+
The **`loading`** property of the {{domxref("HTMLImageElement")}} interface provides a hint to the browser on how to handle the loading of the image which is currently outside the window's {{Glossary("visual viewport")}}. This helps to optimize the loading of the document's contents by postponing loading the image until it's expected to be needed, rather than immediately during the initial page load. It reflects the `<img>` element's [`loading`](/en-US/docs/Web/HTML/Reference/Elements/img#loading) content attribute.
1212

1313
## Value
1414

1515
A string whose value is one of `eager` or `lazy`. For their meanings, see the HTML [`<img>`](/en-US/docs/Web/HTML/Reference/Elements/img#loading) reference.
1616

1717
## Examples
1818

19+
### Basic usage
20+
1921
The `addImageToList()` function shown below adds a photo thumbnail to a list of items, using lazy-loading to avoid loading the image from the network until it's actually needed.
2022

2123
```js

files/en-us/web/api/htmlmediaelement/index.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ _This interface also inherits properties from its ancestors {{domxref("HTMLEleme
4949
- : Returns a boolean that indicates whether the media element has finished playing.
5050
- {{domxref("HTMLMediaElement.error")}} {{ReadOnlyInline}}
5151
- : Returns a {{domxref("MediaError")}} object for the most recent error, or `null` if there has not been an error.
52+
- {{domxref("HTMLMediaElement.loading")}}
53+
- : A string indicating whether the browser should load the media immediately (`eager`) or when it is needed (`lazy`). See [`<video loading>`](/en-US/docs/Web/HTML/Reference/Elements/video#loading) and [`<audio loading>`](/en-US/docs/Web/HTML/Reference/Elements/audio#loading) HTML attributes for more information.
5254
- {{domxref("HTMLMediaElement.loop")}}
5355
- : A boolean that reflects the [`loop`](/en-US/docs/Web/HTML/Reference/Elements/video#loop) HTML attribute, which indicates whether the media element should start over when it reaches the end.
5456
- {{domxref("HTMLMediaElement.mediaKeys")}} {{ReadOnlyInline}} {{SecureContext_Inline}}
@@ -96,9 +98,9 @@ These properties are obsolete and should not be used, even if a browser still su
9698
- : A {{domxref("MediaController")}} object that represents the media controller assigned to the element, or `null` if none is assigned.
9799
- {{domxref("HTMLMediaElement.mediaGroup")}} {{Deprecated_Inline}} {{Non-standard_Inline}}
98100
- : A string that reflects the `mediagroup` HTML attribute, which indicates the name of the group of elements it belongs to. A group of media elements shares a common {{domxref('MediaController')}}.
99-
- {{domxref("HTMLMediaElement.mozAudioCaptured")}} {{ReadOnlyInline}} {{Non-standard_Inline}} {{Deprecated_Inline}}
101+
- `HTMLMediaElement.mozAudioCaptured` {{ReadOnlyInline}} {{Non-standard_Inline}} {{Deprecated_Inline}}
100102
- : Returns a boolean. Related to audio stream capture.
101-
- {{domxref("HTMLMediaElement.mozFragmentEnd")}} {{Non-standard_Inline}} {{Deprecated_Inline}}
103+
- `HTMLMediaElement.mozFragmentEnd` {{Non-standard_Inline}} {{Deprecated_Inline}}
102104
- : A `double` that provides access to the fragment end time if the media element has a fragment URI for `currentSrc`, otherwise it is equal to the media duration.
103105

104106
## Instance methods
@@ -134,9 +136,9 @@ _These methods are obsolete and should not be used, even if a browser still supp
134136

135137
- {{domxref("HTMLMediaElement.captureStream", "HTMLMediaElement.mozCaptureStream()")}} {{Non-standard_Inline}}
136138
- : The Firefox-prefixed equivalent of {{domxref("HTMLMediaElement.captureStream()")}}. See its [browser compatibility](/en-US/docs/Web/API/HTMLMediaElement/captureStream#browser_compatibility) for details.
137-
- {{domxref("HTMLMediaElement.mozCaptureStreamUntilEnded()")}} {{Non-standard_Inline}} {{Deprecated_Inline}}
138-
- : \[enter description]
139-
- {{domxref("HTMLMediaElement.mozGetMetadata()")}} {{Non-standard_Inline}} {{Deprecated_Inline}}
139+
- `HTMLMediaElement.mozCaptureStreamUntilEnded()` {{Non-standard_Inline}} {{Deprecated_Inline}}
140+
- : A non-standard, deprecated, method to capture the steam until it ended.
141+
- `HTMLMediaElement.mozGetMetadata()` {{Non-standard_Inline}} {{Deprecated_Inline}}
140142
- : Returns {{jsxref('Object')}}, which contains properties that represent metadata from the playing media resource as `{key: value}` pairs. A separate copy of the data is returned each time the method is called. This method must be called after the [`loadedmetadata`](/en-US/docs/Web/API/HTMLMediaElement/loadedmetadata_event) event fires.
141143

142144
## Events
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: "HTMLMediaElement: loading property"
3+
short-title: loading
4+
slug: Web/API/HTMLMediaElement/loading
5+
page-type: web-api-instance-property
6+
browser-compat: api.HTMLMediaElement.loading
7+
---
8+
9+
{{APIRef("HTML DOM")}}
10+
11+
The **`loading`** property of the {{domxref("HTMLMediaElement")}} interface provides a hint to the browser on how to handle the loading of the media which is currently outside the window's {{Glossary("visual viewport")}}. This helps to optimize the loading of the document's contents by postponing loading the media until it's expected to be needed, rather than immediately during the initial page load. It reflects the `<audio>` element's [`loading`](/en-US/docs/Web/HTML/Reference/Elements/audio#loading) content attribute or the `<video>` element's [`loading`](/en-US/docs/Web/HTML/Reference/Elements/video#loading) content attribute.
12+
13+
## Value
14+
15+
A string whose value is one of `eager` or `lazy`. For their meanings, see the HTML [`<audio loading>`](/en-US/docs/Web/HTML/Reference/Elements/audio#loading) or [`<video loading>`](/en-US/docs/Web/HTML/Reference/Elements/video#loading) reference.
16+
17+
## Examples
18+
19+
### Basic usage
20+
21+
The `addVideoToList()` function shown below adds a video thumbnail to a list of items, using lazy-loading to avoid loading the video from the network until it's actually needed.
22+
23+
```js
24+
function addVideoToList(url) {
25+
const list = document.querySelector("div.video-list");
26+
27+
const newItem = document.createElement("div");
28+
newItem.className = "video-item";
29+
30+
const newVideo = document.createElement("video");
31+
32+
// Lazy-load if supported
33+
if ("loading" in HTMLVideoElement.prototype) {
34+
newVideo.loading = "lazy";
35+
} else {
36+
// If native lazy-loading is not supported you may want to consider
37+
// alternatives, though this may be fine as a progressive enhancement.
38+
}
39+
40+
newVideo.width = 320;
41+
newVideo.height = 240;
42+
newVideo.src = url;
43+
44+
newItem.appendChild(newVideo);
45+
list.appendChild(newItem);
46+
}
47+
```
48+
49+
## Specifications
50+
51+
{{Specifications}}
52+
53+
## Browser compatibility
54+
55+
{{Compat}}
56+
57+
## See also
58+
59+
- The {{HTMLElement("audio")}} element
60+
- The {{HTMLElement("video")}} element
61+
- [Web performance](/en-US/docs/Learn_web_development/Extensions/Performance) in the MDN Learning Area
62+
- [Lazy loading](/en-US/docs/Web/Performance/Guides/Lazy_loading) in the MDN web performance guide

files/en-us/web/api/window/load_event/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ browser-compat: api.Window.load_event
88

99
{{APIRef}}
1010

11-
The **`load`** event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts (including async, deferred, and module scripts), iframes, and images, except those that are [loaded lazily](/en-US/docs/Web/Performance/Guides/Lazy_loading#images_and_iframes).
11+
The **`load`** event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts (including async, deferred, and module scripts), iframes, and images, except those that are [loaded lazily](/en-US/docs/Web/Performance/Guides/Lazy_loading#images_iframes_video_audio).
1212
This is in contrast to {{domxref("Document/DOMContentLoaded_event", "DOMContentLoaded")}}, which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.
1313

1414
This event is not cancelable and does not bubble.

0 commit comments

Comments
 (0)