[ BLOG ]For Frontend Engineers · 4 min read

Lazy-Load YouTube Embeds Only After a Click

Published

To keep a YouTube player from loading before someone asks to watch, show a local preview and create the iframe after a button press. Keep an ordinary YouTube link available too. This lets the rest of the page work without loading the player, including when JavaScript is unavailable.

Choose between scroll-based and click-based loading

Adding loading="lazy" to an existing iframe asks the browser to defer an offscreen embed until it approaches the viewport. It can load before anyone presses Play. A click-to-load component waits to create the iframe until activation. The web.dev embed guide explains these loading options.

Use native iframe lazy loading when you want the player ready as someone scrolls toward it. Use click-to-load when the video is optional and an extra loading step is acceptable. A visitor may need to press Play inside the loaded player afterward. Label the first button “Load YouTube player” so it describes what actually happens.

If you are still deciding between an embed and hosting your own file, the social-video reuse guide covers that choice.

Start with a local image and a normal link

This example handles one video. Replace VIDEO_ID in both snippets with the ID from the video’s embed URL. Replace the preview path, title, and link text too. Use a frame from a video you own or an image you have permission to publish.

<div id="lesson-video">
  <div data-frame
    style="position:relative;aspect-ratio:16/9;min-height:200px">
    <img src="/images/lesson-preview.webp"
      alt="" width="1280" height="720"
      style="position:absolute;inset:0;width:100%;height:100%;object-fit:contain" />
  </div>
  <button type="button" hidden>Load YouTube player</button>
  <p><a href="https://www.youtube.com/watch?v=VIDEO_ID">
    Watch the lesson on YouTube
  </a></p>
</div>

The empty image alt text avoids repeating the adjacent video link’s description. If the image carries additional information, describe that information instead. The button starts hidden so a visitor without JavaScript sees a preview and a usable link, rather than an inactive control.

Place the component in a column at least 200 pixels wide. The reserved frame has a minimum height of 200 pixels to accommodate narrow layouts. YouTube’s player documentation specifies a minimum viewport of 200 by 200 pixels, with enough room for the controls. Use a link alone if the available space cannot accommodate the player.

Create the iframe on activation

Place this script after the markup. It replaces the preview with a titled iframe, moves focus into it, and leaves the external link available:

<script type="module">
  const component = document.querySelector("#lesson-video");
  const frameBox = component.querySelector("[data-frame]");
  const button = component.querySelector("button");

  button.addEventListener("click", () => {
    const player = document.createElement("iframe");
    player.title = "Lesson: organize a project";
    player.src = "https://www.youtube-nocookie.com/embed/VIDEO_ID";
    player.allow = "encrypted-media; fullscreen; picture-in-picture";
    player.allowFullscreen = true;
    player.style.cssText =
      "position:absolute;inset:0;width:100%;height:100%;border:0";

    frameBox.replaceChildren(player);
    button.hidden = true;
    player.focus();
  }, { once: true });

  button.hidden = false;
</script>

The native button supports keyboard activation. No autoplay parameter is requested, and the iframe is created only once. If you copy this component for several videos, give each instance a unique ID and initialize it separately.

Keep the preview’s requests local

Loading a thumbnail from i.ytimg.com still makes a third-party request before the player exists. This example uses a same-site image and no YouTube API script, preconnect hint, or hover-triggered connection. Check your theme and plugins for resources they may add independently.

Understand the privacy-enhanced domain

YouTube’s privacy-enhanced mode uses youtube-nocookie.com and limits how embedded views are used for personalization. It still loads a third-party player. The local preview delays that contact until activation; changing the embed domain alone does not do that.

Account for video indexing before choosing this pattern

Google’s video indexing guidance says not to rely on user actions to load a video. A player created only after a click may not be discovered as a video on the page. If the page’s purpose is watching that video and video-search visibility matters, use a discoverable player and inspect Google’s rendered page. The watch-page guide covers that decision.

Verify the deployed page

  1. Open the Network panel, disable the cache, and reload. Before activation, check for requests to YouTube, youtube-nocookie.com, ytimg.com, and player-related Google hosts. Investigate any requests rather than assuming the local preview is the only component involved.
  2. Use Tab and Enter or Space to activate the load button. Confirm that exactly one iframe appears, has the correct title and video, and fits the reserved space. Test playback, captions, and full screen in the actual player.
  3. Disable JavaScript and reload. Confirm the preview and YouTube link still work. Also test a blocked embed: the external link should remain available.
  4. Check the narrowest layout on a phone. Compare transferred data before and after activation on your page; another site’s benchmark does not predict your savings.

For a page with several clips, the portfolio video guide covers choosing previews and deciding which videos need to load at all.