← All examples

JavaScript Media substack.com

Substack: auto-pause embedded videos

Pauses HTML5 videos and disables autoplay on embedded YouTube/Vimeo so media does not start on its own.

Code to copy

// Auto-pause embedded video previews so they do not autoplay inside posts
(function () {
  function pause() {
    document.querySelectorAll('video').forEach(function (v) {
      if (v.dataset.jzPaused) { return; }
      v.dataset.jzPaused = '1';
      try { v.autoplay = false; v.removeAttribute('autoplay'); v.pause(); } catch (e) {}
    });
    document.querySelectorAll('iframe[src*="youtube.com/embed"], iframe[src*="player.vimeo.com"]').forEach(function (f) {
      if (f.dataset.jzPaused) { return; }
      var src = f.getAttribute('src') || '';
      if (/(\?|&)autoplay=1/i.test(src)) {
        f.setAttribute('src', src.replace(/(\?|&)autoplay=1/i, function (m) { return m.charAt(0) + 'autoplay=0'; }));
      }
      f.dataset.jzPaused = '1';
    });
  }
  pause();
  var obs = new MutationObserver(function () {
    clearTimeout(window.__jzSubPause);
    window.__jzSubPause = setTimeout(pause, 300);
  });
  obs.observe(document.body, { childList: true, subtree: true });
})();

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. Paste the code into the rule's JavaScript panel and save — it runs on every page visit.

Rate this example

No ratings yet — be the first.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases