Skip to content

Instantly share code, notes, and snippets.

@marklchaves
Created August 23, 2024 11:04
Show Gist options
  • Save marklchaves/bc94b1c499f530d14e3bc8c07f9a8cb1 to your computer and use it in GitHub Desktop.
Save marklchaves/bc94b1c499f530d14e3bc8c07f9a8cb1 to your computer and use it in GitHub Desktop.
Force autoplay=0 (off) for a Vimeo video that opens in an iframe tag.
(function () {
document.addEventListener("DOMContentLoaded", function () { // Wait for all the contents to load.
const elts = document.querySelectorAll("figure a"); // Select all video gallery links.
if (!elts.length) return; // If nothing, bail early.
[...elts].map((elt) => { // Loop over each link.
elt.addEventListener("click", function (e) { // Listen for a click on each gallery link.
console.log("Got a click on a gallery item.");
setTimeout(function () { // Wait for the iframe b/c it loads dynamically.
console.log("Overwriting the autoplay to 0!");
let iElt = document.querySelector("iframe");
console.log(`iframe src before ${iElt.src}.`);
iElt.src = iElt.src.replace("autoplay=1", "autoplay=0"); // Force the autoplay to 0 by overwriting the query param.
console.log(`iframe src after ${iElt.src}.`);
}, 2000); // Tweak this wait time as needed (set to 2 seconds right now).
}); // listener
}); // map
});
})(); // iife
@marklchaves
Copy link
Author

This example code turns off autoplay for Vimeo videos that open in an iframe tag. It's useful if you don't have access to the Vimeo embed code.

You'll need to tweak the timeout as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment