我有其中的主要内容是通过AJAX加载,主“帧”的一个div内部s的系统。
在一些网页上,我需要当用户完成播放视频跟踪。 我试图使用YouTube的iframe API。 它的工作原理确定,但我对一些奇怪的事情正在运行。
主要问题:行动,当用户结束观看视频发生在每一页上不同的,不知何故,API被堆叠的所有功能和所有同时运行。
举例,我的第一页,也就是通过Ajax加载,并且有这样的片段加载YouTube视频:
<script>
// 2. This code loads the IFrame Player API code asynchronously.
if (window.YT)
{
window.YT = undefined;
}
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_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.
var player = undefined;
function onYouTubeIframeAPIReady() {
player = new YT.Player('divVideo', {
playerapiid: 'somecode',
height: '309',
width: '439',
videoId: 'somecode',
events: {
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED ) {
actionToScreen1()
}
}
</script>
它的工作原理确定。 但后来,我加载内容我的两个页面,现在的问题开始:
<script>
// 2. This code loads the IFrame Player API code asynchronously.
if (window.YT)
{
window.YT = undefined;
}
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_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.
var player = undefined;
function onYouTubeIframeAPIReady() {
player = new YT.Player('divVideo', {
playerapiid: 'anothercode',
height: '309',
width: '439',
videoId: 'anothercode',
events: {
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED ) {
actionToScreen2()
}
}
</script>
当我的屏幕2视频播放结束后,onPlayerStateChange被调用了两次,一个叫actionToScreen1等actionToScreen2。 貌似我只是加载通过Ajax的容器,该函数存储有所全球范围内,但我找不到在哪里,并不能找到一个办法来区分被调用哪个函数。 我怎样才能解决这个问题?