视频播放完毕后自动打开链接

时间:2012-06-01 11:37:35

标签: javascript html5

是否可以让html5视频在完成播放后自动打开另一个页面?

如何在javascript中完成?

2 个答案:

答案 0 :(得分:1)

看看media events on MDN

// Cross-browser attach event logic (might be useful for this one day)
function listen(evnt, elem, func) {
    if (elem.addEventListener) { // W3C DOM
        elem.addEventListener(evnt, func, false);
    }
    else if (elem.attachEvent) { // IE DOM
        var r = elem.attachEvent("on" + evnt, func);
        return r;
    }
    else alert('I\'m sorry, I\'m afraid I can\'t do that.');
}

// Attach to event
listen("ended", document.getElementById('myVideo'), DoSomething);

// Event handler
function DoSomething() {
    // Use window.open (new window), or window.location.href = '[your link]'
}​

这是a working fiddle.

修改

由于您在评论中提到您拥有jQuery,因此也可以使用:

$("#myVideo").on("ended", function() {
    // Use window.open (new window), or window.location.href = '[your link]'
});​

这是a working fiddle

答案 1 :(得分:0)

如果您使用的是jQuery,可以这样做:

$("#myVideoElementID").bind("ended", function() {
  // Do stuff here
});