18 September, 2012
0 Comments
1 category
An excellent answer to this question can be found at: http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string
I knew you could call a JavaScript function by it’s string name, by using window[‘functionName’], but this does not work for namespace functions.
When you want to call a namespace function by it’s string name you should use the namespace as context, instead of the window object.
<!DOCTYPE html> <html> <head> <title>General testpage.</title> <script src="/Scripts/Kendo/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> var MyApp = {}; MyApp.Navigation = {}; MyApp.Navigation.refreshDataSources = function () { alert("Alert from refreshDataSources."); }; // Dynamically calling namespace function by string name: MyApp.Navigation["refreshDataSources"](); // Dynamically get namespace part and calling a function in this namespace. MyApp["Navigation"]["refreshDataSources"](); </script> </head> <body> </body> </html>
Tags: Javascript
Category: Uncategorized