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

check if function name which is stored in a variable exist

34
for example in this code:
Expand|Select|Wrap|Line Numbers
  1. var funcName = "myFunc";
  2.  
  3. if (typeof eval(funcName) == 'function') {
  4.   evail(funcName + "()"); // this will call myFunc
  5. }
but its returns runtime error: myFunc does not exist

what im trying to do is call the function specified in the funcName variable (which is myFunc).
I can call is using
Expand|Select|Wrap|Line Numbers
  1. eval(funcName)
but i want to check if it exists before i call it
Sep 24 '07 #1
12 2766
gits
5,390 Expert Mod 4TB
hi ...

welcome to TSDN ...

why do you use eval? ... in case you don't need it ... and i think so ... you may do something like the following:

Expand|Select|Wrap|Line Numbers
  1. function myFunc() {
  2.    alert('test');
  3. }
  4.  
  5. var funcName = myFunc;
  6.  
  7. if (typeof funcName != 'undefined') {
  8.     funcName();
  9. }
  10.  
kind regards
Sep 24 '07 #2
pbmods
5,821 Expert 4TB
Heya, andho. Welcome to TSDN!

Have a look at this article.
Sep 25 '07 #3
andho
34
thanks for the welcome and quick replies. checked that article. but heres my real problem.
i have this HTML:
[HTML]<input type="text" name="single" id="single" onkeydown="validate(event, 'number');" />[/HTML]
On the onkeydown event i call validate function which has two parameters. One is the event and the other is the type of validation to execute, in this case 'number'. It could be varchar or date depending on the how the user wants to use it.
now i need to check if 'number' is a function and then call it:
Expand|Select|Wrap|Line Numbers
  1. function validate(e, type) {
  2.     eval(type + "();");
  3. }
im using eval because the type string could be any validation function in this library
Sep 25 '07 #4
dmjpro
2,476 2GB
Ok!
Have a look at this modified Code.

Expand|Select|Wrap|Line Numbers
  1. function validate(e, type) {
  2.     var command_str = "if(typeof " + type + " == 'function') " + type + "();";
  3.         eval(command_str);
  4. }
  5.  
Good Luck with your try. :-)

Kind regards,
Dmjpro.
Sep 25 '07 #5
gits
5,390 Expert Mod 4TB
hi ...

have a look at the following example that avoids the eval (test it in FF):

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function number(e) {
  3.     alert(e.target.nodeName);
  4. }
  5.  
  6. function validate(e, func) {
  7.     if (typeof window[func] != 'undefined') {
  8.         window[func](e);
  9.     }
  10. }
  11. </script>
  12.  
  13. <input type="button" value="test_button" onclick="validate(event, 'number');"/>
  14.  
kind regards
Sep 25 '07 #6
dmjpro
2,476 2GB
hi ...

have a look at the following example that avoids the eval (test it in FF):

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function number(e) {
  3.     alert(e.target.nodeName);
  4. }
  5.  
  6. function validate(e, func) {
  7.     if (typeof window[func] != 'undefined') {
  8.         window[func](e);
  9.     }
  10. }
  11. </script>
  12.  
  13. <input type="button" value="test_button" onclick="validate(event, 'number');"/>
  14.  
kind regards
hi Gits :-)
Does not eval work in FF.

Kind regards,
Dmjpro.
Sep 25 '07 #7
andho
34
hey it works like a charm, but really the if statement is a little longer but i go with that. But is there a better way if i have a very long if statement
Sep 25 '07 #8
gits
5,390 Expert Mod 4TB
of course it works ... but have a look at it and replace the 'a' in it with an 'i' ... never use eval when there is no need for it ... it evaluates everything and it may lead to the phenomenon to use it for everything ... but in nearly all cases you simply don't need it ...

kind regards
Sep 25 '07 #9
gits
5,390 Expert Mod 4TB
and of course you could use:

Expand|Select|Wrap|Line Numbers
  1. function validate(e, func) {
  2.     if (typeof window[func] == 'function') {
  3.         window[func](e);
  4.     }
  5. }
  6.  
Sep 25 '07 #10
andho
34
hey window[func] is nice. havent seen it anywhere before
how is the compatibility of window[func] with all the browsers.
Sep 25 '07 #11
gits
5,390 Expert Mod 4TB
i think it has to work with all browsers ... its simply using the scope! ... you define your function in the window-context and you may ask the window for the function as its property ... you may consider the window as an array of methods ;)

kind regards

:: while it's an object of course :)
Sep 25 '07 #12
pbmods
5,821 Expert 4TB
Heya, andho.

Have a look at this article.
Sep 25 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
2
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x <...
4
by: Lucas Sain | last post by:
Hi, I think thta for this I have to use reflection... but I'm not shure. How can I find/get an object at runtime by looking for its name that is stored in a variable. For example: I have a...
14
by: Mike Labosh | last post by:
How do you define whether the return value of a function is ByRef or ByVal? I have a utility class that cleans imported records by doing *really heavy* string manipulation in lots of different...
4
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
3
pbmods
by: pbmods | last post by:
AN INTRODUCTION TO FUNCTION OBJECTS LEVEL: INTERMEDIATE PREREQS: OBJECTS You've seen it before. You're setting up an XMLHttpRequest call, and you need to execute a function when it returns, so...
14
by: Dan | last post by:
Hello, we have an intranet application using Windows Integrated Authentification. When an user starts the application, he gets a form for inputting data. The first time he does that, the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.