title: Executing JavaScript code retrieved via AJAX?
body: <p>I've condensed my problem into the following sample code:</p>
  
  <pre><code>&lt;?php 
  if($_GET['ajax']){
      echo '  &lt;script type="text/javascript"&gt;
                  alert("test alert");
                  var newdata="Hello Again!";
                  $(container).append(newdata);
              &lt;/script&gt;';
      exit();
  }
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
          $(document).ready(function(){
              var container = document.getElementById('testdiv');
              var data = 'Hello World' ;
              $(container).append(data);
          });
          function goAjax(){
              $.ajax({
                  url: '/test.php/?ajax=1',
                  type: "POST",
                  data: {"xxx":1},
                  success: function(data){
                      $("#return").empty();
                      $("#return").append(data);
                  }
              });
          };
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;a href="#" onclick="goAjax();"&gt;test&lt;/a&gt;
          &lt;div id="testdiv"&gt;&lt;/div&gt;
          &lt;div id="return"&gt;&lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What I'm trying to do is load in the JS script located at the top of the code into the web page and have it execute.</p>
  
  <p>The <code>alert()</code> executes fine, but I cannot get <code>Hello Again!</code> to show up. It is not executing that part of the script.</p>
  
  <p>Can someone help to explain why?</p>
  
  <p>Note that the use of a reference defined earlier (<code>container</code>) is vital to my problem, and a workaround involving a direct reference does not solve things for me.</p>
  
  <p>Thanks!</p>