473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Call function present in textbox

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! :-)

Nov 2 '07 #1
5 3216
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: "()".
--

Nov 2 '07 #2
Patrick wrote:
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 RunRoutine(sR){
eval(sR)
}
Mick
>
function CalcSum()
{
// .......

}

Thanks! :-)
Nov 2 '07 #3
On 2 Nov, 17:32, Michael White <m...@mickweb.comwrote:
Patrick wrote:
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 RunRoutine(sR){
eval(sR)

}

Mick


function CalcSum()
{
// .......
}
Thanks! :-)- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -
THANKS!!! :-)

Nov 5 '07 #4
Michael White wrote:
Patrick wrote:
>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 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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 8 '07 #5
SAM
Thomas 'PointedEars' Lahn a écrit :
Michael White wrote:
>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)

--
sm
Nov 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ¤ Alias | last post by:
I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ?
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
2
by: shree | last post by:
Hi all, I'm trying to call javascript function via onLoad but running into a problem. I would appreciate any help and suggestion. My application is as follows. When a page loads, I want to check...
0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
4
by: ruca | last post by:
Hi, Can anyone tell me how can I call a function of my Web Page from javascritp???? I have a grid that when I click in a cell I want to call another function present in that same page? It is...
5
by: settyv | last post by:
Hi, Below is the Javascript function that am trying to call from asp:Button control. <script language="javascript"> function ValidateDate(fromDate,toDate) { var fromDate=new Date();
1
by: gabe | last post by:
How do you call a client side javascript callback method after an update panel has posted back to the server? I have two update panels (A + B) with a gridview in each panel. GridView B has a...
4
by: Ty | last post by:
Hello all, I am creating a web site with Visual Stuido 2008. I am trying to use a java script file to create a busybox for login from this page http://blogs.crsw.com/mark/articles/642.aspx. I...
21
by: coolguyaroundyou | last post by:
See the below code: void func(int x) { printf("%d",x); } int main() { int j=0;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.