Controlling YouTube and Vimeo Embeds with JavaScript

8 April 2025

Both YouTube and Vimeo provide JavaScript APIs that allow you to control embedded videos - play, pause, and more. To use these APIs effectively, there are a few setup requirements worth noting.

YouTube

  • Include enablejsapi=1 in the iframe src parameters
  • To use the 'playVideo' method include mute=1 in the iframe src parameters
  • Include the YouTube Player API <script src="https://www.youtube.com/iframe_api"></script>

You can now use the postMessage JavaScript method to execute the Player API methods

YouTube Play/Pause example

Vimeo

  • Include api=1 and in the iframe src parameters
  • To use the 'play' method include muted=1 in the iframe src parameters
  • Include the Vimeo Player SDK <script src="https://player.vimeo.com/api/player.js"></script>

You can now use the postMessage JavaScript method to execute the Player SDK methods

Vimeo Play/Pause example