Created
August 23, 2024 11:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.