Reputation: 5051
I am trying to do something when the user clicks "play" on a YouTube video by using YouTube's API:
<script type="text/javascript">
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
alert('not cool')
if (event.data == YT.PlayerState.PLAYING) {
alert('cool');
}
}
</script>
<iframe id="player" width="970" height="582" src="http://www.youtube.com/embed/6oE-NlWHVuQ?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
It gives the error:
An invalid or illegal string was specified" code: "12
What am I doing work?
Thanks!
Upvotes: 0
Views: 2080
Reputation:
It works on localhost, too as soon as you add the "origin"
parameter to your src
.
If you do write the tag, then when you construct the YT.Player object, you do not need to specify values for the width and height, which are specified as attributes of the tag, or the videoId and player parameters, which are are specified in the src URL. As an extra security measure, you should also include the origin parameter to the URL, specifying the URL scheme (http:// or https://) and full domain of your host page as the parameter value. While origin is optional, including it protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player.
<iframe ... src="http://www.youtube.com/embed/6oE-NlWHVuQ?enablejsapi=1&origin=http://somewebsite.com" ...></iframe>
You can write whatever you want for this value, but you should prefer the domain which you own.
Upvotes: 0
Reputation: 15
The code you have shown works on localhost too, it might be problem with the video you have embedded or the video has disabled the embedding property.
Upvotes: 0