var strFun = "someFunction";
var strParam = "this is the parameter";
//Create the function call from function name and parameter.
var funcCall = strFun + "('" + strParam + "');";
//Call the function
var ret = eval(funcCall);
Code language: JavaScript (javascript)
You can use eval() function to register any kind of JavaScript code like loading a JavaScript file from using AJAX and then registering it with the browser. This way you can load your BIG BIG javascript files on fly by ajax and register it as an when required. UPDATE: It seems that there is a better way to do this without using eval
method. Thanks @Jerome for the comment. Use window[functionName](parameters)
to call any function functionName
. See below example: var strFun = "someFunction";
var strParam = "this is the parameter";
//Create the function
var fn = window[strFun];
//Call the function
fn(strParam);
Code language: JavaScript (javascript)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
How do you send parameters that aren't strings to the function though? Lets say you've got FunctionNameString and Object ... how would you call FunctionNameString(Object) ?
What I found easier is:
// convert string to a function reference (if it exists)
fun = eval(strFun);
// call it
fun(param1, param2);
This worked in IE6 and FF3.5 so I guess it works in others too.
Using this method enables me to use objects as parameters.
It helped me a lot.......
Thanxs.. :)
Good info
Thank You
thx=) help me a lot ;)
You should understand that using of 'eval' is not good enough practice. Happily all js functions are stored in huge 'window' object (if you use js inside browser, of course).
So you can get direct pointer to your function by calling
window['here_is_your_function_name_in_string'];
Or you can even test if this function exists:
window.hasOwnProperty('here_is_your_function_name_in_string');
Ok, complete sample is:
var s = "my_function";
if (window.hasOwnProperty(s)) window[s](params);
Great!! Been looking desperately for this functionality!!!
javascript eval() function
name0,name1,name3 are javascript variables.
jline0,jline1,jline2 are javascript arrays
str="{name : name0,data : jline0},{name : name1,data : jline1},{name : name2,data : jline2 }";
var ff=eval(str);
document.ready(
.
.
series :[ff]
.
.
});
I can't pass ff value in series parameter. Please anyone help me. .
eval is bad, this is the worst way of executing a function.
@Ssam - I agree with you. Using
eval
is not advisable. But in the given situationeval
is only option, where one would like to call a function at runtime using just a name.Correct me if I'm wrong, but this could also be a good solution, provided the only hint you've got on the function to execute is its name :
http://stackoverflow.com/questions/912596/how-to-turn-a-string-into-a-javascript-function-call
I tried this with success :
window[functionName](param);
screw eval !! :D
Jerome, My bad :) I must agree I din't know about this method. Thank you so much for getting this to my attention. I'll update the tutorial right away and add this option as preferred solution.