2

I have three videos that I am trying to detect when they end, and then navigate to another page.

The function seems to only work for the first video $vid1 and nothing happens to the $vid2 and $vid3 videos.

I came across this question, and sourced my code from there.

(I noticed in a comment that it stated that the 'correct' answer doesn't work in chrome)

How would I go about making this work for all videos?

JS

//variables
var $vid1 = document.getElementById("vid1");
var $vid2 = document.getElementById("vid2");  
var $vid3 = document.getElementById("vid3");

//functions
function Redirect1() {
    window.location.replace('ExamplePage1');
}

function Redirect2() {
    window.location.replace('ExamplePage2');
}

function Redirect3() {
    window.location.replace('ExamplePage3');
}

//events  

$vid1.addEventListener("ended", Redirect1, false);
$vid2.addEventListener("ended", Redirect2, false);
$vid3.addEventListener("ended", Redirect3, false);

HTML

<div class="videoResult vidOne">
   <video id="vid1" controls>
     <source src="#" type="video/mp4">
   </video>
</div>

<div class="videoResult vidTwo">
   <video id="vid2" controls>
      <source src="#" type="video/mp4">
   </video>
</div>

<div class="videoResult vidThree">
   <video id="vid3" controls>
      <source src="#" type="video/mp4">
   </video>
</div>
Community
  • 1
  • 1
Jargonaut_
  • 155
  • 1
  • 11
  • How do you know it's not working for the other videos, when the page gets changed as soon as the first video ends? Also note that you define the `Redirect2` function twice. – Rory McCrossan Jul 01 '15 at 08:27
  • Thank you for noticing my typo, I corrected my spelling mistake. And the videos have the controls on them so I have the ability to select which video to play in what ever order @RoryMcCrossan – Jargonaut_ Jul 01 '15 at 08:38
  • But that's my point - the order is irrelevant. As soon as the first video ends (no matter if that's video1, 2, or 3) the page will be redirected. – Rory McCrossan Jul 01 '15 at 08:40
  • That's actually what I want to happen. The videos are on the home page, As soon as the first video ends (whether it be video1, video2 or video3) it will redirect to another page. The page it is being redirected to has the option to navigate back to the home page - hopefully this makes sense @RoryMcCrossan – Jargonaut_ Jul 01 '15 at 09:07

1 Answers1

0

May be, try this?

//functions
function Redirect1() {
    window.location.replace('./ExamplePage1');
}

function Redirect2() {
    window.location.replace('./ExamplePage2');
}

function Redirect2() {
    window.location.replace('./ExamplePage3');
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252