title: How can I dynamically load and execute Javascript from the server without using eval()?
body: <p>I'm writing a <strong>PHP framework</strong> which allows PHP developers to <strong>create ExtJS interfaces</strong> with forms, grids, tabpanels and menus <strong>using PHP classes</strong> only.</p>
  
  <p>In order to create a TabPanel, for example, a <strong>PHP class</strong> is instantiated <strong>with an array of URLs</strong> which <strong>get loaded dynamically</strong> when the user clicks on a tab header. </p>
  
  <p>In order to do this, I use the following Javascript function which <strong>loads a PHP page</strong> via AJAX call and <strong>executes any scripts</strong> inside it.</p>
  
  <pre><code>function loadViewViaAjax(url) {
      Ext.Ajax.request({
          url: url,
          success: function(objServerResponse) {
              var responseText = objServerResponse.responseText;
              var scripts, scriptsFinder=/&lt;script[^&gt;]*&gt;([\s\S]+)&lt;\/script&gt;/gi;
              while(scripts=scriptsFinder.exec(responseText)) {
                  eval(scripts[1]);
              }
          }
      });
  }
  </code></pre>
  
  <p>I often read <a href="https://stackoverflow.com/questions/4500373/is-this-use-of-javascript-eval-100-safe">as in the answers to this question</a> that there is <strong>usually no need to use eval()</strong> since what you need to do with eval() can be usually be <strong>achieved in others ways</strong>. I also understand that executing scripts within a PHP page loaded via AJAX presents a security risk that would need to be locked down in other ways, so I would like to find another, safer way to do this if possible.</p>
  
  <p><strong>What would be an alternative way to dynamically load and execute javascript from the server without eval(), so that I have the same functionality as I do now with the above script, i.e. TabPanels which load and execute Javascript from the server only when the tab headers are clicked?</strong></p>