for example in this code: - var funcName = "myFunc";
-
-
if (typeof eval(funcName) == 'function') {
-
evail(funcName + "()"); // this will call myFunc
-
}
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
but i want to check if it exists before i call it
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: - function myFunc() {
-
alert('test');
-
}
-
-
var funcName = myFunc;
-
-
if (typeof funcName != 'undefined') {
-
funcName();
-
}
-
kind regards
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: - function validate(e, type) {
-
eval(type + "();");
-
}
im using eval because the type string could be any validation function in this library
Ok!
Have a look at this modified Code. -
function validate(e, type) {
-
var command_str = "if(typeof " + type + " == 'function') " + type + "();";
-
eval(command_str);
-
}
-
Good Luck with your try. :-)
Kind regards,
Dmjpro.
gits 5,390
Expert Mod 4TB
hi ...
have a look at the following example that avoids the eval (test it in FF): -
<script type="text/javascript">
-
function number(e) {
-
alert(e.target.nodeName);
-
}
-
-
function validate(e, func) {
-
if (typeof window[func] != 'undefined') {
-
window[func](e);
-
}
-
}
-
</script>
-
-
<input type="button" value="test_button" onclick="validate(event, 'number');"/>
-
kind regards
hi ...
have a look at the following example that avoids the eval (test it in FF): -
<script type="text/javascript">
-
function number(e) {
-
alert(e.target.nodeName);
-
}
-
-
function validate(e, func) {
-
if (typeof window[func] != 'undefined') {
-
window[func](e);
-
}
-
}
-
</script>
-
-
<input type="button" value="test_button" onclick="validate(event, 'number');"/>
-
kind regards
hi Gits :-)
Does not eval work in FF.
Kind regards,
Dmjpro.
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
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
gits 5,390
Expert Mod 4TB
and of course you could use: -
function validate(e, func) {
-
if (typeof window[func] == 'function') {
-
window[func](e);
-
}
-
}
-
hey window[func] is nice. havent seen it anywhere before
how is the compatibility of window[func] with all the browsers.
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
|
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 <...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |