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

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 9821
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==='function' )
--
ivo
http://4umi.com/web/javascript/

Jan 17 '06 #2
la***********@zipmail.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***********@zipmail.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
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
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
by: rashmi | last post by:
how to use static function defined in one file in another file is that impposiible in 'c '
12
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
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
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...
22
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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,...
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
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...

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.