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 Answers 2

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">

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

3 Comments

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.
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.
... and if it's just one pageload per YouTube call, then I see even less issue.
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/

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.