ma***********@hotmail.com wrote:
Hi,
I am looking to call a javascript function using variable number of
parameters.
Suppose I have a function
foo(param)
{
....
}
I want it to be called unpredictebly when certain action occurs.
Not really sure what you're after here, but this seems to be related to
the -arguments- object.
Every function object has an arguments object that is created when the
function is called. arguments is a list of the arguments that were
passed to the function, each argument can be accessed by index.
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Using_the_arguments_obje ct>
For example, I am using AJAX to make server call and get some data.
When I make the call, I also pass the name of my function in thi case
i.e. "foo". the server return some data and the name of the
client-function I originally passed in htis case i.e. "foo".
When the server call returns a callback funciton is invoked where I
process all data returned by server. Now when the callback function is
about the finish, I want it to call whatever function I passed to the
server, passing some data to it.
Are the parameters string literals or variables? i.e. are you trying to do:
foo('strVar0','strVar1','strVar2',...)
or
foo(var0,var1,var2,...)
or some combination of the above?
You might mean that if foo() makes the call, it can include its own name
in the data sent to the server. Later you want to call foo but you have
its name as a string, so somewhere in your code you have:
var funcName = 'foo';
// Later, call whatever function name was assigned to funcName:
window[funcName]();
Though it might be safer to do:
var funcRef = window[funcName];
if ('function' == typeof funcRef){
funcRef();
}
Assuming of course that foo() is a function declared with global scope.
If the script may run in some environment other than a browser, there
is a chance that there is no window object, so you'll want to use a more
generic reference to the global object. Right at the start of the
script do something like:
var _global = this;
then replace all instances of 'window' above with '_global'.
Does any of that help?
--
Rob
Group FAQ: <URL:http://www.jibbering.com/faq/>