0

How do to listen on ended event even when URL is changed?

when the page loads then the player.on ended works, but when i change the url then the ended event doesn't work?

How do i fix this?

<script src="https://player.vimeo.com/api/player.js"></script>
<div class="embed-responsive embed-responsive-16by9">
<iframe src="https://player.vimeo.com/video/137857207" name="frame1" id="frame1" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
<a href="https://player.vimeo.com/video/137857207" onclick="return loadIframe(this.href);">Page 1</a>
<a href="https://player.vimeo.com/video/344456217?h=e78b006de0" onclick="return loadIframe(this.href);">Page 2</a>


<script>
function loadIframe(url) {
$("#frame1").attr('src',url);

return false; 

}
</script>

<script>
var iframe = document.getElementById('frame1');
var player = new Vimeo.Player(iframe);

player.on('play', function() {
 //  alert('You have played the video')
});
player.on('ended', function(){
<?php       
echo "alert('Video play completed')";       
?>  
});

player.getVideoTitle().then(function(title) {
console.log('title:', title);
}); 

</script>
Daniel
  • 51
  • 1
  • 8
  • this happens because a new window is loading within the iframe, any older event listeners will be automatically lost as the older DOM is now scrapped. When you change the URL you would need to reattach the event listeners. – Sumit Surana Aug 12 '22 at 09:13
  • how can I reattach the event listener? – Daniel Aug 12 '22 at 09:17
  • as soon as you change the URL inside iframe, you would need to rerun the whole script again `var iframe = document.getElementById('frame1'); var player = new Vimeo.Player(iframe); // reattach the events` – Sumit Surana Aug 12 '22 at 09:19
  • just want to ask if you can show me the code to do that? – Daniel Aug 12 '22 at 09:43
  • you can use `onLoad` eventListener of iframe to trigger everytime a new iframe loads to reattach the player events. Ref: https://stackoverflow.com/a/48098573/3855179 – Sumit Surana Aug 14 '22 at 06:44

0 Answers0