how to load the Jquery response(HTML page with script tags) into the DOM

后端 未结 2 600
死守一世寂寞
死守一世寂寞 2021-01-26 06:07

test1.html file:






        
相关标签:
2条回答
  • 2021-01-26 06:55

    You need not "load" it into your DOM. For script tags, you could just do..

    eval("<script>alert('Hi!');</script>");
    

    UPDATE :

    If you result contains both HTML and SCRIPT nodes, do the following..

    $.each(nodes, function(i, node) { //result might contain muliple HTML/Script nodes
    
       if (node.tagName == "SCRIPT") {
           eval(node.innerHTML); 
           //Appending to DOM also executes the script. So we do eval() instead, which does the equivalent.
       } else {
           $(node).appendTo($('#returnData')); //And append your HTML nodes to the desired target div
       }
    
    });
    
    0 讨论(0)
  • 2021-01-26 07:02

    you need to do something like this so that it loads script element , so my advise is when you get response replace script string with scri" + "pt

    var code = "<script>alert('Hi!');</scr"+"ipt>";
    $('body').append($(code)[0]);
    
    0 讨论(0)
提交回复
热议问题