Execute JavaScript code stored as a string

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

    If you want to execute a specific command (that is string) after a specific time - cmd=your code - InterVal=delay to run

     function ExecStr(cmd, InterVal) {
        try {
            setTimeout(function () {
                var F = new Function(cmd);
                return (F());
            }, InterVal);
        } catch (e) { }
    }
    //sample
    ExecStr("alert(20)",500);
    

提交回复
热议问题