Is it possible to track how completely the user watched the video? (Reached the end) and only then open the blocks? Using jq

  • what blocks? where to close? - Alex
  • yes any jq actions to perform after watching the video - Alexander
  • those. Do you just have a movie in the frame on the page? - Alex
  • var vid = document.getElementById("myVideo"); alert(vid.ended); HTML5 video. - Maxim Vlasov
  • right, the movie in the iframe - Alexander

1 answer 1

You can, using the Iframe API and the onStateChange event. The onStateChange event returns 0 when playback is over. However, if you need the user to watch the entire video, then the rewind is not taken into account, that is, 0 will be returned when the video reaches the end, whether the user rewinds it or not.


More details about Iframe API and events can be read here:

 // Загружаем Iframe API var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); //Добавляем видео в элемент с ID player var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', events: { 'onReady': onReady //Функция, которая вызовется когда видео загружено } }); } function onReady() { //Событие onStateChange вернет 0 когда воспроизведение окончено player.addEventListener('onStateChange', function(e) { console.log('State is:', e.data); if(e.data == 0){ //Выполняем нужные действия } }); } 
 <div id="player"></div>