0

My AJAX will responds with

<script type="text/javascript" src="js/checkersGame.js" ></script>

among other things. Everything works fine, except the JavaScript is not executed. How do I force it to execute in the response?

My Ajax is as follows:

function loadXMLDoc()
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            if (xmlhttp.responseText == 0) {
                //do nothing
            } else {
                document.getElementById("everything").innerHTML=xmlhttp.responseText;
            }
        }
    }
    xmlhttp.open("GET", "./poll.php", true);
    xmlhttp.send();
}

function checkUpdate() {
    loadXMLDoc();
    setTimeout(checkUpdate, 10000);
}

checkUpdate();
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
starvator
  • 989
  • 1
  • 11
  • 26
  • What do you mean by "javascript is not executed"? What is in that file? – imtheman Apr 13 '14 at 20:16
  • @imtheman The javascript should draw on the canvas on the page. Here is the file: http://users.encs.concordia.ca/~t_ramsa/assignment4me/js/checkersGame.js – starvator Apr 13 '14 at 20:17
  • Do you get any errors in the console? Perhaps it's loading the script before it's loading the html. I'm not sure what you have already and what you are getting from your response. – imtheman Apr 13 '14 at 20:27
  • Script tags contained within HTML that you add by assigning to `.innerHTML` does NOT execute the script inside. If you want script tags to execute, you have to parse them out of your HTML and then manually create script tags and insert them into your document with `.appendChild()`. – jfriend00 Apr 13 '14 at 20:29
  • @jfriend00 could you give me an example? I don't quite understand. – starvator Apr 13 '14 at 21:07
  • This is how a script is added programmatically: http://stackoverflow.com/questions/11160948/how-to-know-if-jquery-has-finished-loading/11161045#11161045. This particular example has a callback too so you can know when it's been loaded and run. – jfriend00 Apr 13 '14 at 21:30

1 Answers1

0

If you're trying to make an ajax call you should be doing something like

<script type="text/javascript">

$.ajax({
  url: "js/checkersGame.js",
}).done(function(reponse) {
  //do something with response (replace content of "everything")
});

</script>

You'll have to change xmlhttp.onreadystatechange to return the new content and then change content of "everything" outside the checkesGame.js script. You might have to rework the timeout logic as well..

slife
  • 21
  • 2
  • The question is not tagged with the jQuery tag; the raw XHR code in the OP is itself not the problem. – Pointy Apr 13 '14 at 20:24
  • The ajax is not calling from checkersGame.js, I want to execute that script in the response, or even after the ajax is complete. – starvator Apr 13 '14 at 20:31
  • yes my bad scanned over the script to fast, the problem is probably due to DOM not loading the script. jfriends got your answer with appendchild – slife Apr 13 '14 at 20:38