Execute JavaScript code stored as a string

前端 未结 20 832
北荒
北荒 2020-11-22 08:58

How do I execute some JavaScript that is a string?

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


        
20条回答
  •  灰色年华
    2020-11-22 09:26

    You can execute it using a function. Example:

    var theInstructions = "alert('Hello World'); var x = 100";
    
    var F=new Function (theInstructions);
    
    return(F());
    

提交回复
热议问题