Can I pass some Javascript to a function and then execute that Javascript from within the function, e.g.
test("a = 1; b = 2; test2(a,b);");
function test(js) {
// execute dynamically generated JS here.
}
Basically I have some code that is generated on the server and I want to pass that code to a JS function which when it has finished processing it executes the code passed as a parameter.
This could also be useful for the parameter of setTimeout, then the code passed could be executed in the timeout event.
Can this be done?
You can do this with
eval(): http://www.w3schools.com/jsref/jsref_eval.aspHowever, be careful that you don’t expose yourself to the security issues.
Why is using the JavaScript eval function a bad idea?