-1

The javascript is not executing the AJAX request, however the script is executed without AJAX. How can I execute scripts with AJAX which would be executed without ajax.

According to the following code I want the last line to be executed

Here I make an AJAX request to script ajax1.php

  document.getElementById("txtHint").innerHTML = this.responseText;
}
xmlhttp.open("GET", "ajax1.php?stateofmachine=xm234jqNewTemplateSelect" + "&q=" + str, true);
xmlhttp.send();  

ajax1.php returns a table with class name as tree

echo "<table class=\"tree\">";
echo "<tr class=\"treegrid-1\">";
echo "<td>Root node</td>
echo  "</tr>";
echo "</table>";            

ajax1.php also returns scripts

echo "<script src=
  \"http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
  \"></script>";
echo "<script type=\"text/javascript\" src=
  \"js/jquery.treegrid.min.js\"></script>";
echo "<script type=\"text/javascript\">$('.tree').treegrid();</script>"; 
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Raghavan
  • 637
  • 3
  • 12
  • 1
    Try with fetch api – Rebai Ahmed Sep 04 '19 at 16:39
  • 2
    has nothing to do with Ajax. The issue is innerHTML does not execute JavaScript. – epascarello Sep 04 '19 at 16:40
  • thanks for the responses. will think about the responses. can you quickly tell me if I can use anything instead of innerhtml. – Raghavan Sep 04 '19 at 17:05
  • 1
    Could you try to only return the last script of your AJAX, I thing you could incluide the scripts of jquery and treegrid in the "Master page" and just get the scripts like this: `";";//your call here ` Do you get an error? could you try with something more basic like an alert? Also use a funtion like document ready in your last script so it could be executed. – Alejandro Gomez Guerrero Sep 04 '19 at 17:11
  • you are very correct in what you said. I have included the scripts in the master page and they did not work. you have indeed been very helpful. I will spend time analysing the various options you have listed. thanks for all the help. – Raghavan Sep 04 '19 at 17:14
  • 1
    See https://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml – Holli Sep 06 '19 at 12:34
  • yeah thats a correct link. very helpful. i instantly understood one of the answers.So use this instead of script tags: ... will try this and tell you the result. – Raghavan Sep 06 '19 at 12:41
  • on first trial did not work from ajax. however i could be wrong. the if (document.readyState == "complete") executes scripts and i moved my script there. this worked for me. thanks for the help. – Raghavan Sep 06 '19 at 12:48

1 Answers1

0

added if (document.readyState == "complete") which executes the scripts..

        xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("txtHint").innerHTML = this.responseText;

            if (document.readyState == "complete")
        {
            //document is ready.do your stuff here
        $('.tree').treegrid();
    }



        }
    };

    xmlhttp.open("GET","ajax1.php?stateofmachine=xm234jqNewTemplateSelect"+"&q="+str,true);
    xmlhttp.send();
Raghavan
  • 637
  • 3
  • 12
  • It would be nice if you explain what you did – Andrei Suvorkov Sep 06 '19 at 12:29
  • the problem was the script returned from an ajax request was not getting executed. xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("txtHint").innerHTML = this.responseText; if (document.readyState == "complete") { //document is ready.do your stuff here $('.tree').treegrid(); } i added if (document.readyState == "complete") and executed the script there. – Raghavan Sep 06 '19 at 12:30
  • You can edit your question by adding additional information and explanation. It would be much more elegant than comment ;) – Andrei Suvorkov Sep 07 '19 at 08:08