最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call jQuery defined function via string - Stack Overflow

programmeradmin0浏览0评论

I'd like to call functions I've defined within the document ready function of jQuery, but am having a bit of trouble. I have the following code:

jQuery(document).ready( function($) {

    function test1() {
        alert('test1');
    }

    function test2() {
        alert('test2');
    }

    var test_call = '2';

    var fn = 'test' + test_call;

    // use fn to call test2

});

I don't want to use eval, and window[fn] doesn't seem to be working. The two test functions don't appear to be indices in the window variable. I appreciate the help and knowledge.

I'd like to call functions I've defined within the document ready function of jQuery, but am having a bit of trouble. I have the following code:

jQuery(document).ready( function($) {

    function test1() {
        alert('test1');
    }

    function test2() {
        alert('test2');
    }

    var test_call = '2';

    var fn = 'test' + test_call;

    // use fn to call test2

});

I don't want to use eval, and window[fn] doesn't seem to be working. The two test functions don't appear to be indices in the window variable. I appreciate the help and knowledge.

Share Improve this question asked Sep 15, 2011 at 0:10 EvanEvan 1,3262 gold badges14 silver badges19 bronze badges 3
  • Are you testing it on jsFiddle or something similar? – Joe Commented Sep 15, 2011 at 0:15
  • 1 window[fn] will not work since you are defining the functions inside a closure so they are not added to the global(window) scope – redsquare Commented Sep 15, 2011 at 0:19
  • Is having one function with a parameter not an option? – Chris Commented Sep 15, 2011 at 0:41
Add a comment  | 

2 Answers 2

Reset to default 12

All I can think of that doesn't use eval() or some form of eval (passing a string to setTimeout() is a form of eval()), is to register the relevant function names on an object and then look up the function name on that object:

jQuery(document).ready( function($) {

    function test1() {
        alert('test1');
    }

    function test2() {
        alert('test2');
    }

    // register functions on an object
    var funcList = {};
    funcList["test1"] = test1;
    funcList["test2"] = test2;


    var test_call = '2';

    var fn = 'test' + test_call;

    if (fn in funcList) {
        funcList[fn]();
    }

});

or the registration could be done in the definition of the functions. If they were global functions, they would be implicitly registered on the window object, but these are not global as they are scoped inside the document.ready handler function:

jQuery(document).ready( function($) {

    var funcList = {};

    funcList.test1 = function test1() {
        alert('test1');
    }

    funcList.test2 = function test2() {
        alert('test2');
    }

    var test_call = '2';
    var fn = 'test' + test_call;

    if (fn in funcList) {
        funcList[fn]();
    }

});

Or, you could move the functions to the global scope so they are automatically registered with the window object like this:

function test1() {
    alert('test1');
}

function test2() {
    alert('test2');
}

jQuery(document).ready( function($) {

    var test_call = '2';
    var fn = 'test' + test_call;

    if (fn in window) {
        window[fn]();
    }

});

The best way, if not Eval, would be to use setTimeout with zero milliseconds, as you can specify the function as a string.

setTimeout('myfunction()',0,);
发布评论

评论列表(0)

  1. 暂无评论