title: Run JavaScript after Ajax
body: <p>I have a HTML that uses this function for loading another PHP with Ajax:</p>
  
  <pre><code>   function LoadContent(n,func)
  {           
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange=function()
      {
          if (xmlhttp.readyState== 4 &amp;&amp; xmlhttp.status == 200)
          {
              document.getElementById("div-content").innerHTML=xmlhttp.responseText;
          if(typeof(func)==='undefined')
                  {
                  // nothing
                  }
              else
                  {
                  eval(func);
                  }
  
          }
      }
      xmlhttp.open("GET",n,true);
      xmlhttp.send();
  }
  </code></pre>
  
  <p>So far, all OK. The PHP to be loaded generates a HTML which contains a </p>
  
  <pre><code>&lt;script&gt; 
  function foo() { 
  alert('hello'); 
  } 
  &lt;/script&gt;
  </code></pre>
  
  <p>And the original HTML calls the loader with <code>javascript:LoadContent('file.php','foo()')</code> , but foo() is not called. The eval() line says "no such function" in Chrome.</p>
  
  <p>How could I manage it so, after Ajax loading, the JavaScript function which would reside in the loaded html would be executed?</p>