I'm trying to use the YouTube API to control several you-tube videos. And I'm following the google code example. But the problem is that they require a global string for their listener callback.
ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
where ytplayer
is the "object" element that contains the flash video player. Following their example it seems I need to create a new global function for each player -- onPlayerStateChange2
, etc. They don't allow function references (anonymous or otherwise) in place of the string, and locally defined strings don't work either. How can I avoid explicitly named global functions? The following works
var localStateChangeHandler = function (newState) {
updateHTML("playerState", newState);
}
var globalStateChangeString = 'onPlayerStateChange'+ytplayer.id;
eval(globalStateChangeString + ' = localStateChangeHandler');
ytplayer.addEventListener("onStateChange", globalStateChangeString);
but it's not very pretty, it's still creating global functions, and is using an "eval". Is there a better way?