javascript - Appending multiple parameters/arguments to a jsonp callback function -
how specify more arguments passed jsonp callback function?
for example, i'm trying grab youtube video data:
http://gdata.youtube.com/feeds/api/videos/gzds-kfd5xq?v=2&alt=json-in-script&callback=youtubefeedcallback
the javascript callback function called youtubefeedcallback , contains 1 argument when called.
as of function this,
function youtubfeedcallback(response) { ... }
what able pass second argument this,
function youtubefeedcallback(response, divid) { ... }
is possible do. i've tried looking everywhere online , couldn't find anything. thanks!
you can't add arguments callback function that. however, can generate wrapper function. jsonp callback function function in default namespace, means need add generated function known name global window
object. step 1 make name:
var callback_name = 'youtubefeedcallback_' + math.floor(math.random() * 100000);
in real world you'd want wrap in loop , check window[callback_name]
isn't taken; use window.hasownproperty(callback_name)
check. once have name, can build function:
window[callback_name] = function(response) { youtubefeedcallback(response, divid); };
you'd want little bit more though:
function jsonp_one_arg(real_callback, arg) { // looping , name collision avoidance left exercise // reader. var callback_name = 'jsonp_callback_' + math.floor(math.random() * 100000); window[callback_name] = function(response) { real_callback(response, arg); delete window[callback_name]; // clean after ourselves. }; return callback_name; }
once have wired up, call:
jsonp = jsonp_one_arg(youtubefeedcallback, divid);
and use value of jsonp
callback
value in youtube url.
you build more functions handle longer arguments lists too. or build general purpose 1 arguments
, apply
.
Comments
Post a Comment