By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,600 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,600 IT Pros & Developers. It's quick & easy.

Check if function exists

P: n/a
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.

Oct 19 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
CWaldman wrote:
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.
--
Rob
Oct 19 '05 #2

P: n/a
Or perhaps

if (typeof myFunctionName=="function")
{

}

Oct 19 '05 #3

P: n/a
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 19 '05 #4

P: n/a

Randy Webb wrote:
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly


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

Oct 19 '05 #5

P: n/a
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.

Oct 19 '05 #6

P: n/a
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?

Oct 19 '05 #7

P: n/a
CWaldman wrote:
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?

--
Rob
Oct 19 '05 #8

P: n/a

CWaldman wrote:
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

Oct 20 '05 #9

P: n/a
CWaldman wrote:
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
Oct 21 '05 #10

P: n/a
RobG wrote:
CWaldman wrote:
How can I check if a function exists ...
<snip> ... . 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.
Oct 23 '05 #11

P: 1
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.
Dunno about JScript, but here's how to do it in vbscript:

Expand|Select|Wrap|Line Numbers
  1. Function ProcExists(ByVal prmProcName) '// Checks if a Sub or Function exists in the script
  2.   Dim obj
  3.   On Error Resume Next
  4.     Set obj = GetRef(prmProcName)
  5.     If Err.Number <> 0 Then
  6.       ProcExists = False
  7.     Else
  8.       ProcExists = True
  9.     End If
  10.   On Error GoTo 0
  11. End Function
  12.  
You use it like so:

Expand|Select|Wrap|Line Numbers
  1. If ProcExists("SubOrFunctionName") Then
  2.   'Do something, e.g. call the Sub or Function
  3. End If
  4.  
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.