How do I execute some JavaScript that is a string?
function ExecuteJavascriptString()
{
var s = \"alert(\'hello\')\";
// how do I get a browser to al
I was answering similar question and got yet another idea how to achieve this without use of eval()
:
const source = "alert('test')";
const el = document.createElement("script");
el.src = URL.createObjectURL(new Blob([source], { type: 'text/javascript' }));
document.head.appendChild(el);
In the code above you basically create Blob, containing your script, in order to create Object URL (representation of File or Blob object in browser memory). Since you have src
property on tag, the script will be executed the same way as if it was loaded from any other URL.