Load javascript code from XMLHttpRequest


I am loading content into a div with XMLHttpRequest, but I want to run pure javascript code to change the contents of another div. I used to work with jquery and it was very simple, but I have not achieved it with pure javascript.

 var req = new XMLHttpRequest();
 function alertContents() {

    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById('insertar').innerHTML = req.responseText;

The html content, texts and those things are loaded but I execute a simple alert (); inside the script tags and nothing. What solution can you give me?

asked by CRUZ VARGAS 26.12.2018 в 23:38

1 answer


I solved it for now by placing variables inside the script tags and locating the variables with indexOf. Then I run javascript with the values I get from the XMLHttprequest. It is not the most efficient but for now it solves the problem. I put something half explanatory:

    var tabla_mostrar = document.getElementById("tabla_mostrar");

    var var_recibida_request = "<script>var variableEnviada='valor-';</script><table></table>";

          tabla_insertar.innerHTML =  var_recibida_request ;

    var inicio = var_recibida_request.indexOf("variableEnviada");

    var fin = var_recibida_request.indexOf("-");

    var var_para_crear_script = var_recibida_request.substring(inicio, fin);
answered by 27.12.2018 в 16:01