1

I need a function to check whether a function in jQuery exists and if that's true this function should be called. If I use below code with an simple name it works, but if i want to check dynamically whether a function exists it doesn't work. Have anybody an idea?

function homeController() {
    console.log('in the homecontroller');
}

$('div[class="view"]').each(function() {
        $('#' + this.id).live('pageshow', function() {
            var func = this.id + 'Controller';              
            if($.isFunction(func)) {
                console.log('jquery exists');
            }
        });
    });
Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
Siggy Petersen
  • 195
  • 3
  • 6
  • 16
  • You mean you want to test whether arbitrary JavaScript functions are in the scope? Of course `func` is just a a string in your example, not a function. – Felix Kling Aug 09 '11 at 13:46

6 Answers6

3

Use the jQuery.isFunction() method to check whether an object is a function or not.

$('div[class="view"]').each(function() {
        $('#' + this.id).live('pageshow', function() {
            var func = eval(this.id + 'Controller');              
            if($.isFunction(func)) {
                console.log('jquery exists');
            }
        });
    });
twernt
  • 20,271
  • 5
  • 32
  • 41
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
2

I think that you need to use eval for that, because your func is a string.

var func = eval(this.id + 'Controller');
if($.isFunction(func)) {
   func();
}
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • If i use ''eval'' it will execute the function and throws an error when a function does not exists, so i need another way for it. – Siggy Petersen Aug 09 '11 at 13:58
  • If you don't put parentheses `()` at the end it will not execute but only get the value of it. – jcubic Aug 09 '11 at 14:03
  • I dont put parentheses at the end, but i get still an error in my case "listController" is not defined. – Siggy Petersen Aug 09 '11 at 14:14
  • 1
    You can try to use `$.isFunction(window[this.id + 'Controller'])` – jcubic Aug 09 '11 at 14:17
  • Ok actually this works only if i check for functions which are outside the `$(document).ready(function()..` scope...so i move my functions out and now it works. Thanks for your help – Siggy Petersen Aug 09 '11 at 16:46
1

$.isFunction - "Determine if the argument passed is a Javascript function object". Obviously, you are passing a string.

If you wish to check for jQuery plugin than use

if (typeof jQuery[yourFunctionName] == 'function') { ..

And for general JavaScript function -

if(typeof window[yourFunctionName] == 'function') { ..
Janis
  • 8,593
  • 1
  • 21
  • 27
0
function doIt(callback) {

    console.log('Did it');

    typeof callback === 'function' && callback();

}

This only executes the callback if the function exists. You can pull that line from this function and use it anywhere.

0

Reflection-java can also be used to check whether a method or a field or a class exists or not, dynamically. for more information: How To Check If A Method Exists At Runtime In Java?

Community
  • 1
  • 1
Renu Yadav
  • 316
  • 1
  • 6
0

You need to figure out which scope your function is in (or in other words, which object are your functions properties of), and then use

if ($.isFunction(scope[func])) {
    ...
}

to convert the name of the required function into a reference to that function within the current scope.

That assumes, of course, that your functions are actually in some scope.

So if your homeController() function has been declared in global scope, it will be a property of the window. global object.

However if they've just been declared as an inner function within some other closure they won't have one.

Alnitak
  • 334,560
  • 70
  • 407
  • 495