24
I need to execute a custom piece of JavaScript I got from some AJAX call. I could do an eval of the string or I could just append it in a script-tag to the DOM. Which method would be better?
var dynamicScript = 'alert(\'Hello world!\');';
Method 1 - Script:
var x = '<script type="text/javascript">' + dynamicScript +'</scr' + 'ipt>';
$(document.body).append(x);
Method 2 - Eval:
eval(dynamicScript);
What method is better and why? Or is there an ever better alternative?