Execute JavaScript code stored as a string

前端 未结 20 830
北荒
北荒 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:04

    Use eval().

    W3 Schools tour of eval. Site has some usable examples of eval. The Mozilla documentation covers this in detail.

    You will probably get a lot of warnings about using this safely. do NOT allow users to inject ANYTHING into eval() as it is a huge security issue.

    You'll also want to know that eval() has a different scope.

提交回复
热议问题