我有这个页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="miscript.js"></script>
</head>
<body>
<div align="center">
<video id="myvideo" autoplay>
<source src="008.mp4" type="video/mp4">
Your browser does not support html5 videos
</video>
</div>
</body>
</html>
我想观看视频,视频结束后我想重定向到其他页面。
我正在尝试这个,但不起作用。
miscript.js
document.getElementById('myvideo').addEventListener('ended',myHandler, false);
function myHandler(e) {
window.location="NewFile1.html";
};
有什么想法吗?
最佳答案
jQuery:
$("#myvideo").bind("ended", function() {
//code to run when video ended
//redirect to http://stackoverflow.com
window.location.replace("http://stackoverflow.com");
});
没有 jQuery:
var videoObj = document.getElementById('myvideo');
videoObj.onended = function(e) {
//code to run when video ended
//redirect to http://stackoverflow.com
window.location.replace("http://stackoverflow.com");
};
通过使用匿名函数添加事件监听器:
document.getElementById('myvideo').addEventListener('ended', function(e) {
//code to run when video ended
//redirect to http://stackoverflow.com
window.location.replace("http://stackoverflow.com");
})
页面重定向:
window.location.replace("http://stackoverflow.com");
或
window.location.href="http://stackoverflow.com";
注意:尝试在 DOM 加载后运行此代码。
$(document).ready(function() { //Run from here });
关于javascript - 完成 html 视频后转到其他页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40300010/