我已經通過WordPress將iframe集成了一些視頻。當YouTube視頻結束時,要捕捉到Javascript事件
我正在尋找的是在視頻播放結束後立即將用戶重定向到主頁。 在現有的YouTube視頻停止時,Javascript中是否有任何事件需要捕獲?
在此先感謝。
我已經通過WordPress將iframe集成了一些視頻。當YouTube視頻結束時,要捕捉到Javascript事件
我正在尋找的是在視頻播放結束後立即將用戶重定向到主頁。 在現有的YouTube視頻停止時,Javascript中是否有任何事件需要捕獲?
在此先感謝。
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
// first video
player = new YT.Player('player', {
events: {
'onReady': function(){ alert("Ready!"); },
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
console.log('player stopped');
}
}
上面的代碼將工作,如果你的iframe看起來如下
<iframe id="player" width="640" height="360" src="https://www.youtube.com/embed/ZdP0KM49IVk?enablejsapi=1" frameborder="0" allowfullscreen />
與id=player
和?enablejsapi=1
檢查這個職位的詳細信息http://stackoverflow.com/questions/10773322/event-當youtube視頻完成 – sathishn
非常感謝。我已經檢查過了。但是我認爲這個帖子是從2012年開始的,現在是2014年的中期。這些年中有2年沒有任何改變?不過非常感謝。你認爲它可以在現有的iframe上運行嗎?不是youtube api生成iframes。 – aumio
此外,如果你能幫我一點點。 「玩家」對象來自哪裏?如何定義玩家對象? – aumio