473,588 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check if a JS function is defined?

Hello,

What is a cross-browser way to check if a function has been defined?

It seems that this code

if (myFn) {
myFn();
}

executes whether myFn is defined or not. When it is not, a JS error
results.

Your help is greatly appreciated, - Dave

Jan 17 '06 #1
3 9827
Ivo
<la***********@ zipmail.com> wrote
What is a cross-browser way to check if a function has been defined?
It seems that this code

if (myFn) {
myFn();
}

executes whether myFn is defined or not. When it is not, a JS error
results.


I don't see how that seems, but have you tried this if statement:
if ( typeof myFn==='functio n' )
--
ivo
http://4umi.com/web/javascript/

Jan 17 '06 #2
la***********@z ipmail.com wrote:
Hello,

What is a cross-browser way to check if a function has been defined?

It seems that this code

if (myFn) {
myFn();
}

executes whether myFn is defined or not. When it is not, a JS error
results.


Use typeof:

function someFn(){}

function isFn(funcRef)
{
return 'function' == typeof funcRef;
}

alert('Is someFn a function? ' + isFn(someFn) );

--
Rob
Jan 17 '06 #3
RobG wrote:
la***********@z ipmail.com wrote:
What is a cross-browser way to check if a function has been defined?
[...]


Use typeof:

function someFn(){}

function isFn(funcRef)
{
return 'function' == typeof funcRef;
}

alert('Is someFn a function? ' + isFn(someFn) );


Note that for host object's methods the `typeof' operation may evaluate to
"object" as well.
PointedEars
Feb 15 '06 #4

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

Similar topics

3
2409
by: Andrew Boothman | last post by:
Hi, This may be the dumbest question ever, but in the following code (using PHP 4.3.8) how do I call b() from within a()? class test { function a() { print "a called"; b();
3
20657
by: lallous | last post by:
Usually you can check for a defined variable as: #ifdef DEF1 // do stuff here #endif Can I test if a function is defined then do things, example: int myFnc() { // blah blah }
5
12176
by: rashmi | last post by:
how to use static function defined in one file in another file is that impposiible in 'c '
12
1798
by: jeniffer | last post by:
Can a static function defined in a C file be ever referred (called) externally from another C file?If so in which conditions?
19
3816
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
2
6732
by: John Smith | last post by:
Hello, I have a VB.NET application with a Windows form that have several textboxes fields where I have dates entered. I would like to do a date validation check after the the field is updated, so I' using the leave event. Right now I am creating a 'leave' sub for each of the fields. However, I'd like to simplify that and just call the name of a function and plug the field name as a variable and be done. In other words, I would like...
22
3978
by: thekingsnake | last post by:
After following the instructions from the answer below: http://bytes.com/topic/javascript/answers/153274-script-iframe-can-not-call-functions-defined-parent-document I was able to have the child window perform the function in the parent window. The function was to only have an alert window pop up as the child page was loading. I easily modified this to my intention which was to have a parent function performed when the child window...
0
7929
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7862
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
8223
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
5729
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
3847
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
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
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
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.