0

Simply trying to redirect after video finishes, works great on all browsers except mobile chrome and safari. Doesn't seem to catch the event, what am I missing?

function playVideo(){

var video = document.getElementById('video');
video.play();
video.addEventListener('ended',function(){
    window.location = 'appt.html';
});
 
}
<video controls id="video"  width="100%" onclick="playVideo()">
<source src="./advisors/intro.mp4" type="video/mp4" />
user1724365
  • 87
  • 2
  • 15
  • Does this answer your question? [inline javascript onclick event](https://stackoverflow.com/questions/11567749/inline-javascript-onclick-event) – Bumhan Yu Jan 19 '23 at 15:17
  • No, unless I am just to obtuse to understand it, lol. – user1724365 Jan 19 '23 at 15:20
  • You have unclosed `` tag. It is not related to this question but it is **invalid**. You can't listen `ended` event inside click function. Use external function for listen `ended` event like [this question](https://stackoverflow.com/questions/2741493/detect-when-an-html5-video-finishes) (see answers) and make it listen once DOM loaded. You don't have to remove `controls` attribute. – vee Jan 19 '23 at 16:35

2 Answers2

1

Turns out the fix was to simply remove the "controls" attribute from "video" element.

user1724365
  • 87
  • 2
  • 15
0

Because your onclick function does not firing on touch devices

function playVideo() {

  var video = document.getElementById('video');
  video.play();
  video.addEventListener('ended', function() {
    window.location = 'appt.html';
  });

}
document.getElementById('video').addEventListener("canplay", playVideo)
<video controls id="video" width="100%">
<source src="./advisors/intro.mp4" type="video/mp4" />
Smollet777
  • 1,618
  • 1
  • 16
  • 15
  • 1
    This JS will make video auto play. To make `ended` independent, move listen `ended` to outside or do the same like `canplay`. – vee Jan 19 '23 at 16:53
  • @vee ofc. Here i'm showing that the problem is the absence of click events on touch screens. I wrongly assumed that it's obvious that video.play() will run when "video can play" – Smollet777 Jan 19 '23 at 17:20