Forum Moderators: open

Message Too Old, No Replies

Function Exists?

JavaScript: determinging the existance of user defined functions.

         

Dromar2003

10:45 pm on Nov 14, 2003 (gmt 0)

10+ Year Member



How do I check to see if a function exists in JavaScript?
My first thought was to see if the function was defined by looking for it in window.

if (window.FUNCTION_NAME)
alert('FUNCTION_NAME is defined!');
else
alert('Bob who?');

I guess this only works for looking up varriables.
Well, I'm out of ideas. How would you do it?

DrDoc

2:25 am on Nov 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can always do something like this:

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
window.onerror = echo;
function echo(err) {
if(err) {
alert("The following JavaScript error occured:\n\n" + err);
return true;
}
}
</script>
</head>

<body>
<a href="javascript:blah()">Error 1</a>
<a href="javascript:alert('test)">Error 2</a>
<a href="javascript:echo()">No error</a>

</body>
</html>