Skip to content
Advertisement

Execute JavaScript code stored as a string

How do I execute some JavaScript that is a string?

function ExecuteJavascriptString()
{
    var s = "alert('hello')";
    // how do I get a browser to alert('hello')?
}

Advertisement

Answer

With the eval function, like:

eval("my script here");
Advertisement