jquery calling a function
yet again thanks for looking.
i like to call a function (eg. comment_disp, post_disp or any other i create later on).
i have created a json function with url, fname and id can i use fname as a function name?
// on document ready runs
json('comments.php','comment_disp');
- url = url to get
- fname = function
- name id = post or any other data id.
json = function(url,fname,id){ $开发者_如何学Python.getJSON(url,(fname)); } comment_disp = function(json){ }
Functions are first-class objects in JavaScript. You can put them into parameters directly, no need to transport their name as string.
So instead of
json = function(url,fname,id){ $.getJSON(url,(fname)); }
do
json = function(url,f,id){ $.getJSON(url, f); }
and call it as
json('comments.php', comment_disp);
Assuming your functions are available in the "global scope" (i.e. not written as a private member of another class/constructor), you can call your functions by string name like this:
window['comment_disp']();
This would be the equivalent of calling
comment_disp();
精彩评论