Given a function name as a string, how could I determine if such function exist (globally), and if yes, call to this function ?
I tried to do:
function foo() {
alert("foo called");
}
var func_name = "foo";
if (typeof window[func_name] == 'function') {
foo();
} else {
alert(func_name + " is not defined!");
}
The reason your jsfiddle doesn’t work is because the named function has been defined within jsfiddle’s default
onLoadwrapper.This means that the function is only defined within the scope of that closure, and isn’t added to the global object.
For testing purposes, just add your function explicitly to the global object by declaring it as: