Executing [removed] injected by innerHTML after AJAX call

前端 未结 11 1584
误落风尘
误落风尘 2020-11-22 05:02

There\'s a div called \"Content\":

It should be filled with data from a PHP file, by AJAX, including

11条回答
  •  感情败类
    2020-11-22 05:17

    Here is the script that will evaluates all script tags in the text.

    function evalJSFromHtml(html) {
      var newElement = document.createElement('div');
      newElement.innerHTML = html;
    
      var scripts = newElement.getElementsByTagName("script");
      for (var i = 0; i < scripts.length; ++i) {
        var script = scripts[i];
        eval(script.innerHTML);
      }
    }
    

    Just call this function after you receive your HTML from server. Be warned: using eval can be dangerous.

    Demo: http://plnkr.co/edit/LA7OPkRfAtgOhwcAnLrl?p=preview

提交回复
热议问题