我试图捕捉一个事件,以便当 youtube 播放器结束时,我可以重定向到另一个网页。但它不起作用。任何人都可以发现任何问题吗?javascript 似乎确实可以编译。
这是整个页面的代码(我实际上是从下面浏览器的查看源页面粘贴的。
它相当简单,事件列在 onYouTubePlayerAPIReady() 函数中,我假设它应该在代码结束时触发。我'我已经输入了“警报”以查看是否有。看起来它没有:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
})
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
function onYouTubePlayerAPIReady() {
player = new YT.Player('iframeid', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
alert("video is over");
location.href= 'k_Origins.aspx';
}
else if (event.data == YT.PlayerState.PAUSED) {
}
else if (event.data == YT.PlayerState.PLAYING) {
}
else if (event.data == YT.PlayerState.BUFFERING) {
}
else if (event.data == YT.PlayerState.CUED) {
}
else {
}
}
</script>
</head>
<body>
<form method="post" action="dostuff.aspx" id="form1">
<div id="ContainPlayer">
<iframe id="iframeid" width="420" height="315"
src="http://www.youtube.com/embed/8X0ZkN_EtTE?hl=en&fs=1&autoplay=1&autohide=0&enablejsapi=1&modestbranding=1&playerapiid=ourvideoid&rel=0"
frameborder="0" ></iframe>
</div>
</div>
</form>
</body>
</html>