I have a string that looks something like this:
var codeStr = "function a(){alert(4);}"
and I want to turn that into a function.
I can do this using eval
or new Function
, for example:
var fn = eval(codeStr);
But when the content-security-policy is active and does not allow "unsafe-eval", this is blocked.
Important note: it's not enough to just run the code string. I need to get the return value into a variable too.
The security of running code from string has been taken into consideration.