Skip to content

Instantly share code, notes, and snippets.

@nickchauhan
Created December 1, 2021 11:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickchauhan/0cf83516379ac7d545698af40b3d6ece to your computer and use it in GitHub Desktop.
Save nickchauhan/0cf83516379ac7d545698af40b3d6ece to your computer and use it in GitHub Desktop.
Pause Videos (HTML video / youtube embed/ vimeo embed) programmatically using Javascript / Typescript
// Get all the videos in the slides
const videos: NodeListOf<HTMLVideoElement> = document.querySelectorAll("video");
const iframeVideos: NodeListOf<HTMLIFrameElement> =
document.querySelectorAll("iframe");
// pause all the HTML videos
if (videos.length > 0) {
videos.forEach((videoItem) => {
videoItem.pause();
});
}
// pause all the iframe videos
if (iframeVideos.length > 0) {
iframeVideos.forEach((iframe) => {
if (iframe.contentWindow) {
// Pause Youtube Videos
if (iframe.src.startsWith("https://www.youtube.com")) {
iframe.contentWindow.postMessage(
'{"event":"command","func":"pauseVideo","args":""}',
"*"
);
}
// Pause Vimeo Videos
if (iframe.src.startsWith("https://player.vimeo.com/")) {
iframe.contentWindow.postMessage('{"method":"pause"}', "*");
}
}
});
}
@EmranAhmed
Copy link

Thank you @nickchauhan. It was really a helpful snippet.

@ShivamJoker
Copy link

thanks for the vimeo one

@mrfrase3
Copy link

😙👌

@lukedaviesSToC
Copy link

Samuel L Jackson voice MA'MAN - thank you

@sekhar-spiderhit
Copy link

super

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