1

I am just starting to get into JavaScript and couldn't find an exact scenario like this yet on SO, so I'm going to try my luck. I have two functions in an external JS file which create video feeds on our website:

function getVideos() {
    //gets a list of videos
}

//callback function automatically called by getVideos()
function response(jsonData) { //can't change this line
    var resp = document.getElementById("resp"); //can change this line and any subsequent lines
    //parses data and populates resp
}

Then, from the HTML side, we just call getVideos() and the video feed will be created and populated.

However, I want to be able to pass any element ID I want into response() so that we can create multiple video feeds in different places on the same page. The thing is I can't change the function declaration of response() to include another parameter. Or at least I'm not led to believe I can by the company hosting our videos.

I've tried wrapping response() with getVideos() and passing an element ID from there, but then response() doesn't get called, and the only solution I can think of is resorting to storing an element ID in a global variable, which I know is a no-no in general in JavaScript.

My question is: Do I just bite the bullet and use a global variable, or is there another way?

For more info, here is our JS code as it stands now (with the closure): http://www.thebearrocks.com/Other/js/videoFeed/createVideoFeed.js And here is the tutorial on response() we're following from the host of our videos: http://support.brightcove.com/en/video-cloud/docs/making-media-api-calls-dynamic-script-tags

  • Welcome to Stackoverflow! There's no need to add tags to your title, there's a tag system for that. Please read http://meta.stackexchange.com/q/19190 for the general discussion. – Patrick Apr 13 '13 at 15:06
  • Thanks for the welcome and tips, Patrick! – user2276325 Apr 13 '13 at 15:58
  • It's not 100% clear what you can change and what you can't. In particular, can you change the `getVideos` function, the composition of the JSON response, and the HTML? – Beetroot-Beetroot Apr 14 '13 at 18:43

2 Answers2

0

may be you can use arguments? like so:

function response(jsonData) { //callback function automatically called by getVideos()
    var elemId = arguments.length<2 ? "resp" : arguments[1]+"";
    var resp = document.getElementById(elemId);
    //parses data and populates resp
}

or, declare second argument what has default value like this:

function response(jsonData, elemId) { 
    elemId = elemId || "resp";
    var resp = document.getElementById(elemId);
    //parses data and populates resp
}

in this case function can be called as with one or two arguments

orrollo
  • 311
  • 1
  • 4
  • Unless I'm mistaken, I don't think I can change response() to accept more arguments than it already has. The tutorial link I put up from the company hosting our videos specifies only one argument for response(). I will look into your first suggestion though. Thanks! – user2276325 Apr 13 '13 at 16:02
0

I've tried wrapping response() with getVideos() and passing an element ID from there, but then response() doesn't get called, and the only solution I can think of is resorting to storing an element ID in a global variable, which I know is a no-no in general in JavaScript.

My question is: Do I just bite the bullet and use a global variable, or is there another way?

No. Not the id variable needs to become global, but your local response function needs to for getting called back from the JSONP script - you're going to create a closure.

You can "export" it by calling

window.response = mylocalResponseFunction; // you did name that local var "response"
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Not sure what you mean by exporting. Can you clarify? I am still a bit new to JS. – user2276325 Apr 13 '13 at 16:12
  • Export it from the local scope where it was declared, so that the function can be called "from outside". There are various possibilities to do so, for example `return`ing it from the scoping function (`createVideoFeed` in your case), adding it as a callback somewhere or - like here - assigning it to a variable in a higher scope. Did you read the linked question on closures? – Bergi Apr 13 '13 at 19:17