473,323 Members | 1,560 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,323 software developers and data experts.

getting a ref to a function with a string

I'm trying to work on something wierd and was wondering, given a string
that contains the name of a function, can you get a reference to that
function? Any pointers are greatly appreciated. TIA.

-f

Jul 28 '05 #1
3 1136
Ivo
"nybble" wrote
I'm trying to work on something wierd and was wondering, given a string
that contains the name of a function, can you get a reference to that
function? Any pointers are greatly appreciated. TIA.


Two possibilities come to mind: one using eval and one looking into the
window's properties, which all global variables including functions and
other objects belong to. The following should explain itself:

<script src="" type="text/javascript">//<![CDATA[

function fun() { window.alert( 'Hi!' ); }

window.alert( typeof window[ 'fun' ] ); // function

var tmp = eval( 'fun' );
window.alert( typeof tmp ); // function
tmp(); // Hi!

//]]></script>

hth
ivo
http://4umi.com/
Jul 28 '05 #2
Lee
nybble said:

I'm trying to work on something wierd and was wondering, given a string
that contains the name of a function, can you get a reference to that
function? Any pointers are greatly appreciated. TIA.


Use square bracket notation to access the function as an attribute
of the Window (assuming you're running this in a browser window):

var myString="alert";
window[myString]("Hello, world!");

Jul 28 '05 #3
Ahhh, perfect! Thanks, I had actually tried that with the document
object, didn't even think to do the lookup on the window object. Eval
wasn't satisfying for what I was trying to do... thanks a lot for the
pointer.

-f

Jul 28 '05 #4

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

Similar topics

5
by: Francis Bell | last post by:
I just found that my fin stream is not getting passed to my readInASpinnerbait function. Here's what I have: string readInFirstChars(ifstream &fin) { char first; string print; while...
21
by: Gerry Abbott | last post by:
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string....
8
by: Salad | last post by:
I designed a small app and I wanted to do a BrowseFolder (see http://www.mvps.org/access/api/api0002.htm), basically do a file open diaglog and select a directory/folder. The problem is that you...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
43
by: chookeater | last post by:
Hi All. My introduction to VB was through VB.Net but I am working with a lot of code that was written by programmers with pre-dotnet experience. As a result there are lots of function calls and...
4
by: ItsMe | last post by:
Hi Guyz, I'm unable to understand this (AddressOf) error??? In VB6 I have two functions: ---------------------------- Public Function ImageFirstImageCallback(ByVal hWnd As Integer, ByVal...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
4
by: AshishMishra16 | last post by:
HI friends, I am using the Flex to upload files to server. I m getting all the details about the file, but I m not able to upload it to Server. Here is the code i m using for both flex & for...
5
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace...
2
by: hgarg | last post by:
The onpaint() is getting called before calculating the required parameters by listBox1_SelectedIndexChanged function. I tried calling it at the end of this function. But of no use. Due to this issue...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.