DEV Community

Discussion on: Lazy load embedded YouTube videos

Collapse
 
jelle619 profile image
Jelle Z. • Edited

YouTube has an JavaScript-based embed API. In HTML, you can defer external JavaScript until the page has finished parsing. This way, the video player will only load after other parts of the page have been loaded first. While this won't decrease the amount of data that will get downloaded or prevent tracking by Google, it will allow people with a poor internet connection to comfortably view the page whilst the video player is taking its time to load in the background.

Here's a CodePen: codepen.io/jelle619/pen/yLBVyWY

If you don't like using this method, you could still make videos automatically play on browsers which, on the surface, don't seem to support autoplay. Some browsers have started blocking autoplay because it allows web pages to play sound without the user's permission which can be unwanted. However, in many instances, browsers that do this will still allow autoplay when the video has been muted.