Load javascript code from XMLHttpRequest

0

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
source

1 answer

0

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:

<script>
    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);
</script>
    
answered by 27.12.2018 в 16:01