Pass an arbitrary number of parameters into a JavaScript function -


my javascript code barely ajax request expects xml returned back-end. back-end can return execute_callback 1 of xml tags this:

<?xml version="1.0" encoding="windows-1251"?> <response>     <execute_callback>         <function_name>somefunction</function_name>     </execute_callback> </response> 

and okay far know exact number of parameters callback expects. if back-end has returned

<?xml version="1.0" encoding="windows-1251"?> <response>     <execute_callback>         <function_name>somefunction</function_name>         <param>10.2</param>         <param>some_text</param>     </execute_callback>     <execute_callback>         <function_name>otherfunction</function_name>         <param>{ x: 1, y: 2 }</param>     </execute_callback> </response> 

how pass parameters 10.2 , 'some_text' somefunction , json { x: 1, y: 2 } otherfunction?

i know ugly solution (using function's arguments), looking pretty one.

and before forget: don't parse xml me - can on own :) need of trick pass arbitrary number of arguments function in javascript. if know python, want:

def somefunc(x, y):     print x, y args = { 'x' : 1, 'y' : 2 } somefunc(**args) 

but in javascript.

you refer function.apply. assuming callback functions declared in global object (window in browser).

var callback_function = window[function_name]; if (callback_function) { // prevent calling undefined functions     callback_function.apply(window, params);  // params array holding parameters } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -