title: run javascript function from string variable and pass parameters
body: <p>I need to call a user defined javascript function from within my costom jquery plugin and pass parameters to it, for example:</p>
  
  <pre><code>function test(data)
      {
        var myfunc="function(data){alert(data);}"; //this is user defined function I   retrieved from html tag attribute
        var fn=new Function("("+myfunc+")();");
        fn.apply(this,arguments);
        return fn;
  } 
  test("hello");
  </code></pre>
  
  <p>The result is undefined, how can I pass data parameter from test function to user defined function? thanks in advance!</p>
  
  <p>question update:</p>
  
  <p>I'm writing a jquery plugin to handle ajax request, much like asp.net mvc unobtrusive ajax, I get the ajax callfack function from html tag attrbute, for example:</p>
  
  <pre><code>&lt;div data-ajax-success="function(data,status,xhr){alert(data);}"....
  </code></pre>
  
  <p>the value of data-ajax-success attribute is user defined function, it can be following formats:</p>
  
  <pre><code>data-ajax-success="function(data,status,xhr){alert(data);}"
  data-ajax-success="function(data){alert(data);}"
  data-ajax-success="function(){alert('hello');}"
  data-ajax-success="functionName"
  </code></pre>
  
  <p>I need to parse this attribute value as javascript function and pass jquery ajax callback parameters to this function, where data-ajax-success value is function name, I could call it correctly using following method defined in Micrsoft jquery-unobtrusive-ajax.js:</p>
  
  <pre><code>function getFunction(code, argNames) {
          var fn = window, parts = (code || "").split(".");
          while (fn &amp;&amp; parts.length) {
              fn = fn[parts.shift()];
          }
          if (typeof (fn) === "function") {
              return fn;
          }
          argNames.push(code);
          return Function.constructor.apply(null, argNames);
      }
  </code></pre>
  
  <p>but when data-ajax-success is function body, I could not pass parameter to it, here's my sample  code that handle ajax callback:</p>
  
  <pre><code>loadData: function (index, options) {
  complete: function (xhr,status) {
              $(context.loading).hide(context.loadingDuration);
              getFunction(context.onComplete, ["xhr", "status"]).apply(this, arguments);
              },
          success:function (data, status, xhr) {
              $(context.updateTarget).html(data);
              getFunction(context.onSuccess, ["data", "status", "xhr"]).apply(this, arguments);
              },
              error: getFunction(context.onFailure, ["xhr", "status", "error"])
  });
  
        $.ajax(options);
    }
  </code></pre>
  
  <p>anyone can help me? thank you very much!</p>