Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass function name as variable

Status
Not open for further replies.

Bastien

Programmer
May 29, 2000
1,683
CA
Hi All,

I am playing with some AJAx code and want to pass a fuunction name into my function to allow the response from the ajax request to be passed to another function for more processing...i can't figure out the correct parameters to do this:

Code:
ajax(url, data, doSomething);
//doSomthing is the variable that can take an object (like a div) a function to call another function or "" (empty string which causes an EVAL to be run on the response)



Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
something like this, perhaps?

Code:
<head>
<script language="javascript">
function sayHello(arg1){
 	alert ('Hello ' + arg1);
}
function dosomething(funcName,arg1) {
 	funcName(arg1).call; //use the call function
}
</script>
</head>
<body onload='dosomething(sayHello, "John");'>
 
excellent, thanks

the solution we came up with in the meantime was

var myobjName = function myFunction(params);



Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
and, of course, you could use eval() if you trusted the input.
 
sorry - for other readers my earlier code 'worked' but was syntactically incorrect. use this instead:

Code:
function dosomething(funcName,arg1) {
 	funcName.call(this,arg1);
}
 
jpadie, the call method is actually completely unnecessary:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function blah(str) {
   alert(str);
}

var a = blah;

a("test");

</script>
<style type="text/css"></style>
</head>
<body>

</body>
</html>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
thanks kaht. good to know!

i'm curious as to why it's provided then. is it just backwards compatibility?
 
Probably for the same reasons that call(fnName) was a function in old BASIC builds when just using fnName worked. Javascript is a funny language, there's about 6 different ways to do almost everything, and that's before you even get into cross-browser coding or IE only directives.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top