0

As you can see on my site http://www2.outofscopes.com/test/index.html

I'm trying to make the youtube API inform me of any event that is in onStateChange by alerting it. using the function below:

function onYouTubePlayerAPIReady() {
  player = new YT.Player('player', {
    videoId: '-cSFPIwMEq4',
    events: {
      'onStateChange': function (event) {

        alert(event.data);
      }
    }
  });
}

But both in chrome and firefox(mostly firefox), it alerts only sometimes when reloading the page.

Why would that happen?

2
  • not sure, but i think before set the function, you need to wait until frame load. Commented May 18, 2012 at 20:09
  • 1
    @siniradam what do u mean by before set the function? Commented May 18, 2012 at 20:11

1 Answer 1

1

You may try this. http://jsfiddle.net/NrHdq/

        <iframe src="http://www.youtube.com/embed/-cSFPIwMEq4" onload="floaded()" title="YouTube video player" id="player" allowfullscreen="" frameborder="0" height="400" width="900"></iframe>

<script>
    //onYouTubePlayerAPIReady
    function floaded() {
        player = new YT.Player('player', {
            videoId: '-cSFPIwMEq4',
            events: {
                'onStateChange': function (event) {

                    alert(event.data);
                }
            }
        });
    }

</script>

</body>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.