1

I am trying to redirect to a different URL after a html video has ended. This cannot be using a FLASH player as it won't work on iOS devices. Any help or direction will be appreciated. Here is my code:

<head>
<script type="text/javascript">
    myVid = document.getElementById("video1");
    function redirect()
    { 
        if (myVid.ended == true) {
            window.location = "http://www.google.com";
        }
</script>
</head>
<body> 
    <video id="video1" controls="controls">
        <source src="video.mp4" type="video/mp4">
        Your browser does not support HTML5 video.
    </video>
</body> 

1
  • myVid.addEventListener('onended', function(e) { window.location.href = 'http://www.google.com'; }); ?
    – putvande
    Nov 15, 2013 at 19:14

4 Answers 4

4

set the id of your video to 'myvid' and here is the solution

video = document.getElementById('myvid');
video.addEventListener('ended',function() {alert('video is ended');       
window.location.href = 'http://www.google.com';})

here is the demo

4
  • I am still a little stuck. The video did not redirect after it was finished playing. Nov 15, 2013 at 19:37
  • @CAI_Innovator let me know for this
    – Hichem
    Nov 15, 2013 at 20:16
  • @CAI_Innovator on jsfiddle can not redirect but ir works fine i have tested it
    – Hichem
    Nov 15, 2013 at 20:17
  • @K3mel31 Thank you I got it to work. It was a matter of moving the script out of the head tag and into the body tag. I appreciate your help. Nov 16, 2013 at 20:19
0

use window.location.replace('http://www.google.com'); so user dosnt get stuck in back button loops

<script>
    var video1 = document.getElementsByTagName('video1')[0];

    video1.onended = function(e) {
        window.location.replace('http://www.google.com');
    }
</script>
0

Javascript:

 document.getElementById('video-id').addEventListener('ended',function(){
     window.location.href = 'http://www.your-url.com';
 },false);

jQuery:

  $("#video-id").on("ended", function() {
       window.location.href = 'http://www.your-url.com';
    });

Youtube:

How to detect when a youtube video finishes playing?

1
  • this code is perfectly working on chrome and edge but why it is not working on firefox??? Oct 7, 2022 at 6:10
0

This worked for me :

<script>
  function videoEnded() {
      location.href="https://www.google.com";
   }
</script>

<video id="myVideo" width="640" height="480" controls autoplay onended="videoEnded()">
  <source src="video.mp4" type="video/mp4" />
    Your browser does not support the video tag.
</video>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.