0

In JS or Jquery, is it possible to convert a string into a function, and run it ?

Something like this :

var s = 'var n = 0; n++; alert(n)';
convertStringToFunction(s); // this would show an alert with the content '1';

My actual case is not this, it's just a simplified example. What i'm trying to achieve is a way to generate a JS code in runtime and execute it.

Thanks !

4
  • 2
    maybe you are trying to achieve this? Commented Apr 17, 2018 at 19:26
  • Like eval(s)? Commented Apr 17, 2018 at 19:27
  • Exactly. Thanks ! Commented Apr 17, 2018 at 19:27
  • 2
    Sounds like a potential debugging nightmare. Commented Apr 17, 2018 at 19:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.