I have in a json object which stores a JavaScript say
var jsonObj = {
"js": "var x=1;\n callback(x);"
}
Now to execute this JavaScript, I need to provide an implementation for callback. Lets say our callback just prints the value x.
How then am I supposed to make the call using jsonObj.js such that the given javascript executes and prints the value of x (callback prints the value).
I tried the eval function but either my syntax was wrong or perhaps eval is not used here at all.
Any help is appreciated. Thanks!!
Note: If it helps, I am actually writing a firefox plugin using gwt. This peace of code is written as a native JavaScript function.
Assuming you want to pass in the
callback, you’d use theFunctionconstructor instead ofeval, and define the parameters.Then you can pass the function argument:
http://jsfiddle.net/HWCVD/
The
Functionconstructor is designed such that the last argument you pass it is the body of the function, and all the preceding arguments are the parameters to the function.So if you want to add additional parameters, you’d just give them to the
Functionconstructor.Then pass the arguments:
http://jsfiddle.net/HWCVD/16