How to check if function exists in JavaScript?

Published: 03 April 2024
on channel: Code Samples
17
0

My code is
function getID( swfID ){
if(navigator.appName.indexOf("Microsoft") != -1){
me = window[swfID];
}else{
me = document[swfID];
}
}

function js_to_as( str ){
me.onChange(str);
}

However, sometimes my onChange does not load. Firebug errors with

me.onChange is not a function

I want to degrade gracefully because this is not the most important feature in my program. typeof gives the same error.
Any suggestions on how to make sure that it exists and then only execute onChange?
(None of the methods below except try catch one work)


Watch video How to check if function exists in JavaScript? online without registration, duration hours minute second in high quality. This video was added by user Code Samples 03 April 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 17 once and liked it 0 people.