title: Execute javascript function on a page sent via AJAX response
body: <p>I want to execute a function that is sent over AJAX request from the server. The function body isn't in the calling page. For example: (the complete code is given below)</p>
  
  <p>1.calling PHP script:</p>
  
  <pre><code>&lt;script&gt;
   function fun()
   {
      try{
          // Opera 8.0+, Firefox, Safari
          ajaxRequest = new XMLHttpRequest();
      } catch (e){
          // Internet Explorer Browsers
          try{
              ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
              try{
                  ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e){
                  // Something went wrong
                  alert("Your browser broke!");
                  return false;
              }
          }
      }  
   ajaxRequest.onreadystatechange = function(){
          if(ajaxRequest.readyState == 4){
  
              document.getElementById("rslt").innerHTML = ajaxRequest.responseText;
  
  
          }
      }
  
      ajaxRequest.open("GET", "ajax_js2.php", true);
      ajaxRequest.send(null);
  
   }
  &lt;/script&gt;
  &lt;input type="button" onclick="fun()"&gt;
  &lt;div id="rslt"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>2.ajax_js2.php script:</p>
  
  <pre><code>&lt;script&gt;
   function test()
   {
     alert("Hello");
   }
  &lt;/script&gt;
  &lt;span onclick="test()"&gt;Test AJAX&lt;/span&gt;
  </code></pre>
  
  <p>I know putting the function definition of "test" in the calling script will do. But I want to keep it in the second script.What should I do ? The server returns the span as a response to the client. While clicking upon the span "Test AJAX" the function should be called.</p>