0

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..

1 Answer 1

1

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 }}];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can u please explain how is it different?
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")
Whoever downvoted it: What about a comment? It's pretty rude to downvote a correct answer without mentioning a reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.