0

I'm utilizing the Youtube Data API, and following the guide for JSON. Youtube recommends that you utilize a "script" tag to call the service and you specify a callback function. Instructions provided here: https://developers.google.com/youtube/2.0/developers_guide_json.

EXAMPLE

<script type="text/javascript" 
    src="http://gdata.youtube.com/feeds/users/GoogleDevelopers/uploads?callback=showMyVideos&v=2&alt=json-in-script&format=5">
</script>

Which would call:

function showMyVideos(data) {...} 

Notice the query string in the script tag specifies the parameter "callback=showMyVideos".

QUESTION

My question is whether its possible to pass through a variable to my callback function that I manually specify?

What I would like to do is something like "callback=showMyVideos(id,data)"

which would call:

function showMyVideos(id,data){...}

where "id" is set by me in the tag, and the "data" is what is returned by the call to youtube.

The reason I need to do this is so I can insert the Youtube video that is returned from the Youtube API to the the div with the id that I'm passing through.

2 Answers2

1

Create a wrapper function which calls your normal callback with the specified variables. Then give your wrapper function as the YouTube callback.

<script type="text/javascript">
    function showMyVideos123() {
       showMyVideos(1, 23);
    }
</script>

<script type="text/javascript" 
    src="http://gdata.youtube.com/feeds/users/GoogleDevelopers/uploads?callback=showMyVideos123&v=2&alt=json-in-script&format=5">

andytuba
  • 187
  • 9
  • The problem with this is that for each youtube "script" call I need to specify a different ID... so with this solution it would be equivalent to having to programmatically write a new showMyVideo function everytime I call youtube, since there are potentially millions of IDs I need to pass through. – HelpMeStackOverflowMyOnlyHope Jan 03 '13 at 22:09
  • Why is that a problem? If you intend to load potentially millions of YouTube videos in the same page load, one measly extra function for each is the least of your worries. – andytuba Jan 05 '13 at 03:55
  • ... and if it's just one pageload per YouTube call, then I see even less issue. – andytuba Jan 05 '13 at 20:18
0

Instead use the youtube API with callback use it searching videos via AJAX. Here is a sample showing both options: http://acuriousanimal.com/blog/2011/11/02/using-youtube-api-to-embed-videos-on-your-web-site/

acanimal
  • 4,800
  • 3
  • 32
  • 41