I've some videos integrated through WordPress as iframe.
What I'm looking for is to redirect users to the homepage as soon as the video finishes playing.
Is there any event in Javascript to catch when existing Youtube video stops?
I've some videos integrated through WordPress as iframe.
What I'm looking for is to redirect users to the homepage as soon as the video finishes playing.
Is there any event in Javascript to catch when existing Youtube video stops?
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
// first video
player = new YT.Player('player', {
events: {
'onReady': function(){ alert("Ready!"); },
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
console.log('player stopped');
}
}
Above code will work if your iframe looks as below
<iframe id="player" width="640" height="360" src="https://www.youtube.com/embed/ZdP0KM49IVk?enablejsapi=1" frameborder="0" allowfullscreen />
with id=player
and ?enablejsapi=1