PDA

View Full Version : Calling a function from string variable


arekanderu
11-25-2007, 10:53 PM
Hello all

is it possible to make something like the following?

var functionString = "functioName(" + parameter + ")";
functionString();

Of course the above does not work, i get error that functionString() is not a function so i was wondering if someone knows the solution to this. Google didn't help

Regards

Trinithis
11-25-2007, 11:56 PM
var f = new Function("a", "b", "c", "return a * b * c;");
alert(f(2, 3, 4)); //24


To be honest, you should never create a function in this fashion.

(Yeah, yeah, yeah . . . never say never. But let's be realistic.)

Same goes for non-ajax eval(): Don't use it or anything like it such as setTimeout("string"). It is slow, error prone, and can produce unexpected results.

me2
11-26-2007, 01:04 AM
var functionString = "functioName(" + parameter + ")";
functionString();


try taking off the parenthesis at the end of functionString


var functionString = "functioName(" + parameter + ")";
functionString;


the variable calls the function not the function calling it if you want to call it using the function declare the funtion
[code]
function functionString(parameter){
alert(parameter)
}

arekanderu
11-26-2007, 09:08 AM
Hello and thank you for your replies.

me2 unfortunately what you suggest does not work (did you try it and get different results?) which i find it logic since without the () functionString is interpreted as string


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Untitled 1</title>

<script language="javascript" type="text/javascript">
function test()
{
alert("called test");
}

var functionString = "test()";
functionString;
</script>
</head>
<body>
</body>
</html>


The code i originally posted i expected to work since it's done in the same way in PHP:


<?php
function foo() {
echo "In foo()<br />\n";
}

function bar($arg = '')
{
echo "In bar(); argument was '$arg'.<br />\n";
}

// This is a wrapper function around echo
function echoit($string)
{
echo $string;
}

$func = 'foo';
$func(); // This calls foo()

$func = 'bar';
$func('test'); // This calls bar()

$func = 'echoit';
$func('test'); // This calls echoit()
?>


Trinithis, i do not want to create a function in that way. The function already exists and i just want to build it up because the parameters passed are not known until a user presses a specific button. In more detail what i do is the following:

1. A form with input text areas exists on the page.
2. User fills the form, presses submit
3. On submit i gather the values of all inputs and create an array with them.
4. I use the array as parameter of a function which already exists.

In order to call the function though i must build it because the parameters are not known to me until the submission. That's why i am trying to do it in the way i post it.

In PHP this can be done (as you can see above) but Javascript is not really my thing

arekanderu
11-26-2007, 10:50 AM
Found it =)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Untitled 1</title>

<script language="javascript" type="text/javascript">

function test()
{
alert("inside test");
}

var functionString = "test";
window[functionString]();

</script>

</head>

<body>

</body>

</html>