javascript HTML 视频结束后如何重定向到 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20008778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:30:49  来源:igfitidea点击:

How can I redirect to a URL after a HTML video has ended?

javascripthtmlurlredirectvideo

提问by CAI_Innovator

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:

我正在尝试在 html 视频结束后重定向到不同的 URL。这不能使用 FLASH 播放器,因为它不适用于 iOS 设备。任何帮助或方向将不胜感激。这是我的代码:

<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> 

回答by K3rnel31

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

将视频的 ID 设置为“myvid”,这是解决方案

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

here is the demo

这是演示

回答by naw103

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

使用 window.location.replace(' http://www.google.com'); 所以用户不会陷入后退按钮循环

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

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

回答by HIRA THAKUR

Javascript:

Javascript:

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

jQuery:

jQuery:

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

Youtube:

YouTube:

How to detect when a youtube video finishes playing?

如何检测youtube视频何时播放完毕?