22 Sept 2015

Calling JavaScript function from String

Calling JavaScript function from String:

==============================
Sometime you want to call a Javascript method at runtime whos name you know. Let say there is a method “foo()” which you want to call at runtime. Below is a small JS snippet that helps you calling a method just by its name.
var strFun = "someFunction"; //Name of the function to be called
var strParam = "this is the parameter"; //Parameters to be passed in function
 
//Create the function
var fn = window[strFun];
 
//Call the function
fn(strParam);

Share:

0 comments:

Post a Comment