Currently, I am facing an issue with my page that has multiple embedded YouTube players. I need to listen for events on these players but encounter a strange error while using a solution mentioned in the answer found at Using Youtube's javascript API with jQuery. The error I receive is as follows (observed in Chrome 18.0.1025.137 beta-m):
Uncaught SyntaxError: Unexpected token %
This error occurs when the onStateChange
event is triggered. My code snippet looks like this:
var onYouTubePlayerReady = function (id) {
var evt = '(function(){})';
alert(eval(evt)); //just to ensure syntactical correctness of the snippet
var ytplayer = document.getElementById(id);
ytplayer.addEventListener("onStateChange", evt);
};
(for more details, refer to the linked question)
The error occurs specifically when the onStateChange
event is fired. If I set evt
to an empty string or a function name, the error does not occur (but unfortunately, no state information is received either).
While it seems like the error message is misleading, does anyone know if what I am attempting to achieve is actually possible?