Is this a valid function pointer code below,

In views ,

    def change(request):
       dict={}
       function_ptr="create()" 

        dict.update({'function_ptr' : function_ptr})
       return render_to_response('mpjt/create.html',context_instance=RequestContext(request,{'dict': dict}))

In create.html

      $(document).ready(function() {
     var a = '{{dict.function_ptr}}'

     func_ptr(a);

      function create()
     {
       alert('got respponse');
      }
     });

Thanks..

share|improve this question

76% accept rate
feedback

1 Answer

up vote 1 down vote accepted

No.

Pass function_ptr='create' in your python code and use the following in your JavaScript:

var func_ptr = window[{{ dict.function_ptr }}];

This must be done AFTER the function create() has been defined! If you want to do it earlier, you could do it with an anonymous function:

var func_ptr = function() {
    return window[{{ dict.function_ptr }}];
}
share|improve this answer
Can u please explain how is it different? – Hulk May 11 '10 at 18:22
window['funcname'] is the proper way to get the function pointer from a string. A string containing 'funcname' or 'funcname()' is just a plain string - not a function (pointer). And while you could execute 'funcname()' with eval() that's very dirty ("eval is evil") – ThiefMaster May 11 '10 at 18:33
Whoever downvoted it: What about a comment? It's pretty rude to downvote a correct answer without mentioning a reason. – ThiefMaster May 11 '10 at 20:34
Thanks................. – Hulk May 12 '10 at 5:29
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.