Check if function exists

C

CWaldman

How can I check if a function exists in any of the scripts on my page?
I prefer if it can work for both Javascript & VBScript.
 
R

RobG

CWaldman said:
How can I check if a function exists in any of the scripts on my page?
I prefer if it can work for both Javascript & VBScript.

I have no idea about VBscript. For JavaScript, in the vast majority of
cases:

if (myFunctionName) {
// use myFunctionName
}


will do the trick. However, if it's possible that 'myFunctionName' has
been declared or converted to some type other than a Function (say
Number, String or Array), then you might use (lightly tested in Firefox
and IE):

if (myFunctionName && Function == myFunctionName.constructor) {
// use myFunctionName
}


Avoid using try..catch, it's generally considered a last resort.
 
L

Lee

CWaldman said:
How can I check if a function exists in any of the scripts on my page?

The reference to "any of the scripts..." seems to make it worth
pointing out that all Javascript functions are loaded into the
same name space in the window, regardless of whether they are
defined in separate script blocks or downloaded from different
files on the server.
 
R

Randy Webb

Baconbutty said the following on 10/19/2005 7:13 AM:
Or perhaps

Or perhaps you learn to quote what you are replying to? It is not that
difficult.

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
 
B

Baconbutty

Randy said:
Baconbutty said the following on 10/19/2005 7:13 AM:

Or perhaps you learn to quote what you are replying to? It is not that
difficult.

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

Ok thanks. Normally I remember. Sorry to slip up this time.
 
C

CWaldman

Thanks to everyone for their help. This still doesn't help me figure
out if a VBScript Sub or Function exists though. This is important
since I don't know which language the author of the page will use. Any
ideas?
 
R

RobG

CWaldman said:
Thanks to everyone for their help. This still doesn't help me figure
out if a VBScript Sub or Function exists though. This is important
since I don't know which language the author of the page will use. Any
ideas?

Have you tried microsoft.public.scripting.vbscript?
 
J

Julian Turner

CWaldman said:
Thanks to everyone for their help. This still doesn't help me figure
out if a VBScript Sub or Function exists though. This is important
since I don't know which language the author of the page will use. Any
ideas?

One option may be use try...catch statements in Javascript, to try
calling the Sub or Function, and to catch any resulting error if the
function does not exist.

I.e.

try
{
MyVBFunction();
}
catch(e)
{
alert("Error.");
}


At worst, "typeof MyVBFunction" results in "unknown", which at least
tells you that the Identifier "MyVBFunction" is not undefined.

Julian
 
T

Thomas 'PointedEars' Lahn

CWaldman said:
Thanks to everyone for their help. This still doesn't help me figure
out if a VBScript Sub or Function exists though. This is important
since I don't know which language the author of the page will use. Any
ideas?

All scripts share the same global namespace with the MS Script engine,
so Rob's suggestion should help. All other script engines don't support
VBScript at all.


PointedEars
 
R

Richard Cornford

... . For JavaScript, in the vast majority
of cases:

if (myFunctionName) {
// use myFunctionName
}

will do the trick.
<snip>

Unfortunately it will not do the trick whenever there is a possibility
that the function that may be referred to by - myFunctionName - is
completely missing (the definition of the function is absent) as an
unqualified Identifier that does not resolve as a named property of some
object on the scope chain results in an internal Reference type with a -
null - "Base" object. Any attempt to read a value using such a Reference
will produce a runtime error, and a type-converting test includes an
implicit attempt to read a value using a Reference type (ECMA 262 3rd
edition: sections 8.7 and 10.1.4).

A type-converting test applied to a proeprty of an existing object will
not have the same problem, so - if(window.myFunctionName){ ... } - would
not be problematic, otherwise a - typeof - test is most appropriate for
unqualified Identifiers.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top