title: Is it possible to execute JavaScript within an AJAX response that gets generated in the server's PHP code?
body: <p>I want to output a time zone next to a date time value from PHP, only after the rows have been sent to the client.</p>
  <p>I compose the time zone statements as such, but need to display them only on empty cell values.  I did not succeed with coalescing nulls to the empty string after the date time.   I want to display nothing if the cell is blank.</p>
  <p>How do I execute the JS block in code after transmission to client, should I be placing the execution result in the client page JS?  Is this at all possible in the first place?   Note: timeouts did not help to delay execution.</p>
  <p><strong>JS PHP Statement String</strong></p>
  <pre><code>&lt;?php
              $jsTimeZoneStatements .=&quot;if ($('td#plug$idx span').text().trim() != '') $('span#timezonePlug$idx').html(getTimezoneName());if ($('td#unplug$idx span').text().trim() != '') $('span#timezoneUnplug$idx').html(getTimezoneName());&quot;;
              $idx++;
  ?&gt;
  </code></pre>
  <p><strong>Script Under HTML Table that does not seem to run or execute immediately after client receives (this is via an AJAX call).</strong></p>
  <pre><code>&lt;script&gt;
      var script = &quot;&lt;?php echo &quot;&lt;code&gt;(()=&gt;{$jsTimeZoneStatements})();&lt;/code&gt;&quot;?&gt;&quot;;
      $('#deviceHistoryScript').append(script);
  &lt;/script&gt;
  </code></pre>
  <p><strong>Unrolled (What AJAX gets from server)</strong></p>
  <pre><code>&quot;...(stuff above this is HTML content)...&lt;div id=\&quot;#deviceHistoryScript\&quot;&gt;
  
      
  
  &lt;/div&gt;
  
  &lt;script&gt;
  
      var script = \&quot;&lt;code&gt;(()=&gt;if ($('td#plug0 span').text().trim() != '') $('span#timezonePlug0').html(getTimezoneName());if ($('td#unplug0 span').text().trim() != '') $('span#timezoneUnplug0').html(getTimezoneName());if ($('td#plug1 span').text().trim() != '') $('span#timezonePlug1').html(getTimezoneName());if ($('td#unplug1 span').text().trim() != '') $('span#timezoneUnplug1').html(getTimezoneName());if ($('td#plug2 span').text().trim() != '') $('span#timezonePlug2').html(getTimezoneName());if ($('td#unplug2 span').text().trim() != '') $('span#timezoneUnplug2').html(getTimezoneName());)();&lt;/code&gt;\&quot;;
  
      $('#deviceHistoryScript').append(script);
  
  &lt;/script&gt;&quot;
  </code></pre>