Using Youtube's javascript API with jQuery

前端 未结 5 1100
萌比男神i
萌比男神i 2020-12-08 03:04

I\'m currently trying to use the YouTube API as part of a jQuery plugin and I\'ve run into a bit of a problem.

The way the YT api works is that you load the flash pl

5条回答
  •  猫巷女王i
    2020-12-08 03:26

    Edit:

    Apparently calling addEventListener on the player object causes the script to be used as a string in an XML property that's passed to the flash object - this rules out closures and the like, so it's time for an old-school ugly hack:

    function onYouTubePlayerReady(playerId) {
        var player = $('#'+playerId)[0];
    
        player.addEventListener('onStateChange', '(function(state) { return playerState(state, "' + playerId + '"); })' );
    }
    
    function playerState(state, playerId) {
        console.log(state);
        console.log(playerId);
    }
    

    Tested & working!

提交回复
热议问题