Call function present in textbox

P

Patrick

Hi! I want kwnow if it's possible to call a function giving an another
function the name (in string format).

Something like this:


var sRoutine = window.Form1.Text1.value; //for exampre the text value
"CalcSum()"


RunRoutine(sRoutine);


function RunRoutine(sR)
{
//here the function call CalcSum()

}

function CalcSum()
{
// .......

}

Thanks! :)
 
L

Lee

Patrick said:
Hi! I want kwnow if it's possible to call a function giving an another
function the name (in string format).

Something like this:


var sRoutine = window.Form1.Text1.value; //for exampre the text value
"CalcSum()"


RunRoutine(sRoutine);


function RunRoutine(sR)
{
window[sR]();


}

In a browser, global functions are attributes of the window object.
You can access an attribute by name using the "square bracket" notation,
then invoke the function by applying the invocation operator: "()".


--
 
T

Thomas 'PointedEars' Lahn

Michael said:
function RunRoutine(sR){
eval(sR)
}

Kids, don't try this at home! It's the worst possible way to do it, as it
will allow for arbitrary code injection.

Assuming that the function to be called is declared global, the following is
safer:

var _global = this;

function runRoutine(sR)
{
sR = sR.match(/^[^\(]+/)[0];

if (typeof _global[sR] == "function")
{
_global[sR]();
}
}

runRoutine(sRoutine);


PointedEars
 
S

SAM

Thomas 'PointedEars' Lahn a écrit :
Michael said:
function RunRoutine(sR){
eval(sR)
}

Kids, don't try this at home! It's the worst possible way to do it, as it
will allow for arbitrary code injection.

Assuming that the function to be called is declared global, the following is
safer:

var _global = this;

function runRoutine(sR)
{
sR = sR.match(/^[^\(]+/)[0];

if (typeof _global[sR] == "function")
{
_global[sR]();
}
}

runRoutine(sRoutine);

I dind't understand :
- the regexp (witch returns only the name of the function)
- the [0] in sR.match(...)[0];
it seems that works without [0]
- what this runRoutine() is supposed to do (better and saffer)

I've tested the following code :

<html>
<script type="text/javascript">
var _global = this;
function runRoutine(sR) {
sR = sR.match(/^[^\(]+/)[0];
if (typeof _global[sR] == "function") { _global[sR](); }
return false;
}
</script>
<form onsubmit="return runRoutine(this.txt.value);">
<textarea name=txt>alert('hello')</textarea>
<input type=submit value=GO>
</form>
</html>

and ... of course ... I obtain in Firefox :
Erreur : uncaught exception: [Exception... "Not enough arguments"

More :
- adding in script : function oo() { alert('hello'); }
- changing textarea's content with : oo
result = the alert 'hello' fires
while I din't enter a complete function as oo()

With in textarea : oo(); ooo();
the alert fires too (without the [0] in sR matching)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top