detecting if a JavaScript function exists
Date: 02/14/06
(Javascript Community) Keywords: java, web
Hi,
I'm currently developing a web application that will run on various systems and some systems will have a certain JavaScript function and others will not. I need to run an if statement to detect if the function exists, and if so, run it. I don't wish to use the onerror function because that would suppress other errors from users and unfortunately the try/catch block only runs under JavaScript 1.5. I've tried many variations of the following code, but all of them either work only when the function exists or when it does not exist. Do any of you know of a nice backwards-compatible way to do this or of a nice workaround?
function chucktest() {
alert("I run.");
}
if ( document.chucktest ) {
chucktest();
} else {
alert("Function does not exist.");
}
Thanks in advance for any help you can provide!
Source: http://community.livejournal.com/javascript/92744.html