eval() doesn't execute external (src=…) scripts

后端 未结 2 546
遥遥无期
遥遥无期 2021-01-29 10:43

I\'m using eval() to execute all

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-29 11:14

    This tag :

    
    

    has empty innerHTML, so you cant eval the content

    What you could do is to check if the script has an src attribute, and add a script tag in the head with this src :

    function addHeadScript = function(src){
        var head = document.getElementsByTagName("head")[0];
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = src;
        head.appendChild(script);
    }
    
    ...
    
    
    for (var n = 0; n < arr.length; n++){
        if (arr[n].src != ""){
            addHeadScript(arr[n].src);
        }
        else {
            // Evaluate the script `innerHTML`   OR
            // Create a script tag in the head and set it's content equal to arr[n].innerHTML
        }
    }
    

提交回复
热议问题