Skip to main content

Block YouTube embeds

Goal: stop a YouTube embed from loading until the visitor consents, and show a placeholder with an Allow button in its place.

YouTube sets cookies as soon as the iframe loads. The iframe itself has to be held back, not just the cookies it would set.

Tag the iframe

Use data-os-src instead of src and pick the category that matches your declaration. YouTube is usually advertising, but use functional if you embed in privacy-enhanced mode and your declaration classifies it that way.

<iframe data-os-category="advertising"
data-os-src="https://www.youtube.com/embed/VIDEO_ID"
width="560" height="315"
title="YouTube video player"
allowfullscreen></iframe>

Because the real URL lives in data-os-src, the browser cannot load the iframe before OptSens decides whether it is allowed. When the category is consented, OptSens restores src and the video loads.

The placeholder

While blocked, OptSens shows a placeholder overlay in place of the iframe with the provider name, the category label, and an Allow button. The visitor can consent to that one category straight from the overlay, and the video loads in place.

Automatic blocking without tagging

If the scanner detected YouTube on your site and iframe blocking is on for the domain, embeds already in your HTML are blocked without any tagging. OptSens removes the src, stores the original, and shows the placeholder. Manual tagging is the way to guarantee a specific embed is blocked, and the only reliable way for iframes added after page load.

Turn on iframe blocking and review providers under Iframe rules.

Iframes added after page load

If you inject embeds dynamically, call runScripts() after inserting them, and OptSens processes the new tagged elements:

container.innerHTML = newHtml;
OptSens.runScripts();

Verify

  1. Open your site in a private window and reject the embed's category.
  2. The placeholder shows and no request goes to youtube.com.
  3. Click Allow on the placeholder.
  4. The video loads in place and the network request to YouTube appears.