How to run javascript code represented by a string? -


thorugh ajax receive random string(built @ runtime on server) contain javascript code like:

plugins.add('test', function()  {          return             {                 html: '<div>test</div>',//edited                 width: 200             }  });//edited 

in client want able execute code. tried using eval function this

eval("(" + str + ")"); 

but error. removed "\r\n" , removed last ";"(semicolon) , after "eval" function succeeded. but, if add comment code above, "eval" fails.

how can run code string? thank you

new function(str)() 

or json:

new function('return ' + str)(); 

if happens fit needs better eval. it's still evil eval.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -