473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing a function name to another funtion

I have a system that I want to try, which requires me, from an aspx
page, to call a generic function to work perform one task, then i want
to call another function with the results of that task.

something like this:

in my aspx page:
<input type=button onClick="doFirs tFunction('seco ndFunctionName' );">

then in my .js file:

var functionToCall = '';

function doFirstFunction (functionName) {

functionToCall = functionName

// do the initial, common task
sResult = xmlHttp.respons eText

//now call the second function, passing sResult to it

}

Right now I'm using 'switch' and manually adding possible values for
'functionName' then directing manually, but I'd like to make
"doFirstFunctio n" self-maintaining, i.e. without having to update it if
I add more 'second' functions later.

I thought I could use

eval('functionT oCall('+sResult +')');

but this doesn't work so far.

any ideas?

Apr 21 '06
17 8577
VK wrote:
Thomas 'PointedEars' Lahn wrote:
VK wrote:
> They are "accessible as named properties of the window object" (I hope
> that this diplomatic answer covers both IE and non IE situations wich
> are rather different).


It covers /only/ the _HTML environment_ in _known_ _graphical_
_Web browsers_, which is its major flaw.


The statement "window host object and Global object are the same"
covers only non-IE based browsers.


It covers exactly _no_ browser.
PointedEars
Apr 21 '06 #11
VK

Captain Dondo wrote:
How do I test for existence of
such an entity?

if(window["myFunction ']) window["myFunction "](a,b,c);


Within the conditional checks JavaScript resolves empty strings, 0,
null, undefined and NaN to false.

Non-empty strings, non-zero numbers and references of any kind are
resolved to true.

If property doesn't exist, you'll get undefined which is covered by the
list above. So the frequently used way is:

// undefined gets resolved to boolean false:
if (window["myFunction "]) { window["myFunction "](); }
else { alert("Function doesn't exists"; }

Apr 21 '06 #12
VK wrote:
Captain Dondo wrote:
How do I test for existence of such an entity?

if(window["myFunction ']) window["myFunction "](a,b,c);
Within the conditional checks JavaScript resolves empty strings, 0,
null, undefined and NaN to false.


Evaluated, not resolved. And that goes for all ECMAScript implementations .
Non-empty strings, non-zero numbers and references of any kind are
resolved to true.
Evaluated, not resolved.
If property doesn't exist, you'll get undefined which is covered by the
list above. So the frequently used way is:
Probably used by /you/. Along with your other numerous failed attempts at
proper programming.
// undefined gets resolved to boolean false:
if (window["myFunction "]) { window["myFunction "](); }
else { alert("Function doesn't exists"; }


However, much better is:

// in global context
var _global = this;

// anywhere "after" the above
if (typeof _global["myFunction "] == "function")
{
_global["myFunction "]();
}
else
{
window.alert("F unction does not exist");
}

Under the provision that `myFunction' is declared globally.

And the bracket property accessor is not necessary at all
if you already know the identifier: _global.myFunct ion

If all the code is in global context, one can use `this'
explicitly and does not have to declare the `_global'
variable. And `typeof myFunction' would suffice then.
PointedEars
Apr 21 '06 #13
Randy Webb <Hi************ @aol.com> writes:
VK said the following on 4/21/2006 4:22 PM:
<input type="button" onclick="firstF unction(secondF unction);">
....
function firstFunction(p tr) {
// do stuff
ptr();
}


ptr is undefined unless that is the name of the function.


ptr is a parameter (and therefore a local variable) of firstFunction.
It is declared in the context where it is used, and will have a value
if one is passed to firstFunction, as it appears from the usage
that one is.
It's not undefined, and it's independent of the name of the function
being passed.

This is the best way to pass functions to other functions: as values.
Passing their names and requireing an explicit lookup is much more
fragile, less readable, requires the passed function to be globally
availalbe, and is all together unnecessary if you have the function
available where you call the first function.
<input type="button" onclick="firstF unction('second Function');">

That is the only way to do it that you have mentioned. And the apos'
have to be on the initial function call.


That's the only way to pass a function's name, which was what to OP
asked for.
It's definitly not the only way to have one function call a second,
where which second function is called is controlled by a parameter of
the first, which perhaps was what to OP really wanted.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 22 '06 #14
I finally got back around to this, and found that this solution worked
perfectly. Manty thanks VK

VK wrote:
Alternatively (if you prefer handle strings) all existing functions are
tunelled through the window object, so:

<input type="button" onclick="firstF unction('second Function');">
...
function firstFunction(s tr) {
// do stuff
window[str]();
}


May 8 '06 #15
Kevin Blount wrote:
I finally got back around to this, and found that this solution worked
perfectly. Manty thanks VK
Please don't top-post.

VK wrote:
Alternatively (if you prefer handle strings) all existing functions are
tunelled through the window object, so:


No, they aren't[1].

Functions may be accessed through a property of the global object where
they are declared in a global scope or the value of a global property is
assigned a reference to a function (e.g. using function expression).

In browsers, the window object is equivalent to the global object, but
not all user agents are browsers and not all have a window object.
1. What does 'tunelled' mean in a JavaScript context? The correct
spelling is 'tunneled', and when used in an IT or computing context
refers to the encapsulation of one network protocol inside another. It
is normally used to transport a network protocol across a network that
otherwise wouldn't support it. I don't see how that applies to
JavaScript at all.
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/faq/>
May 10 '06 #16
RobG wrote:
[...]

1. What does 'tunelled' mean in a JavaScript context? The correct
spelling is 'tunneled'


'Tunneled' is the US spelling, 'tunnelled' is the English spelling
(Oxford dictionary).
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/faq/>
May 10 '06 #17
Please don't top post? whatever man..

May 10 '06 #18

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

Similar topics

3
14952
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10181
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
0
3322
by: Zlatko Matiĉ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding pass-through queries and parameters. I wanted to have all the code on client, while executing it on...
4
5029
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double *b, double *c), double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB) { ...../*other code*/ func(a, b, c); ..... }
2
1801
by: nagle.p | last post by:
Ok, Im writting a plug-in for a application and the class the I have to derive from has a warp() funtion that gets called that is part of the base class. I need to pass a varible into this funtion that has a sequence of numbers starting at (0,0) and going up to (nWidth, nHeight) which would be all the pixels on there screen at each coordinate. So I have tried all different types of things (pointers, functions inside the function call...
5
1945
by: Lee Xuzhang | last post by:
/* from SICP -- Exercise 4.21: ((lambda (n) ((lambda (fact) (fact fact n)) (lambda (ft k) (if (= k 1) 1 (* k (ft ft (- k 1))))))) 10) */
5
25131
by: steven_orocos | last post by:
Hi, I'm tryin to pass a funtion as an argument in another funtion. This code works ---------------------------------- typedef void (*func)(int); void test1(int a){cout<<"1";} void test2(int a){cout<<"2";}
13
2727
by: masso600 | last post by:
char word; in = fopen("test.txt", "r"); while(fscanf(in,"%s",&word)!=EOF) { /* Print all words */ /* printf("%s\n",&word); */
1
4890
by: shalabh6 | last post by:
Hi, A js file containing a function which is passing an alphanumeric string to another function in the same file, the second funtion requires 4 parameters to pass from the first function. The first three are the number and the last one is the alphanumeric string , but when I apply the fourth parameter , it throws out an error Expected';' The value in the varval parameter are like '2 xyz', '4 abc' The calling fucntion is being called...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.