Skip to content

Instantly share code, notes, and snippets.

@jessedc
Last active June 17, 2018 20:59
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jessedc/5344465 to your computer and use it in GitHub Desktop.
Save jessedc/5344465 to your computer and use it in GitHub Desktop.
Autoplay Vimeo Videos from outside their iframe using Javascript.
<html>
<head></head>
<body>
<!-- NOTE: ?api=1 and player_id at the end of the URL -->
<iframe id="player" width="" height="" src="http://player.vimeo.com/video/62207569?api=1&player_id=player" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<script src="http://a.vimeocdn.com/js/froogaloop2.min.js"></script>
<script>
var player = $f(document.getElementById('player'));
player.addEvent('ready', function() {
player.api('play');
});
</script>
</body>
</html>
<html>
<head></head>
<body>
<iframe width="" height="" src="http://player.vimeo.com/video/62207569 " frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<script>
//NOTE: Jesse Collis github.com/jessedc - this works as of it's last updated date. YMMV
// Add a listener for the window's load event
window.addEventListener('load', onWindowLoad, false);
function onWindowLoad(event) {
//use a loop to continue checking the vimeo iframe for the 'play' button
checkForTrue(isIframeButtonLoaded, iFrameButtonLoaded);
}
function checkForTrue(func, callback) {
setTimeout(function() {
if (func()) {
callback( iFrameButton() );
} else {
checkForTrue(func, callback);
};
}, 1000);
}
//finds the 'play' button within the Vimeo iframe
function iFrameButton() {
//window.frames[0].document.getElementsByTagName('div')[1]; // this also works
return window.frames[0].document.getElementsByTagName('button')[5];
}
//simple boolean condition to check for the iframe button
function isIframeButtonLoaded() {
return ( iFrameButton() != null );
}
//once the button is loaded, click it
function iFrameButtonLoaded(iFrameButton) {
iFrameButton.click();
}
</script>
</body>
</html>
@GinoF
Copy link

GinoF commented Sep 30, 2015

Hey Jesse,
I'm trying to do something similar with the iframe player, but I consistently get cross origin issues:
Blocked a frame with origin "http://myurl.here" from accessing a cross-origin frame.

How did you get this working?

@maheshsriram
Copy link

Hi jesse,
This code works perfect in all browsers but not in ipad or iphone. Any solutions?

@mootari
Copy link

mootari commented Nov 8, 2016

@maheshsriram Autoplay without user interaction is generally not possible on mobile devices.

@boldrack
Copy link

it only works on firefox not chrome

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