Ajax div update to insert JS for subsequent asynchronous request to external site I wanted to use the postscribe library (https://github.com/krux/postscribe) to make an asynchronous request to google trends, which would update a div with a graph without requiring a page reload.

Using the library on its own through a few methods (drupal_add_js or a custom block) works just fine, but I wish for the js code to be reloaded everytime the appropriate ajax callback is initiated.

$form['trends-visual'] = array(
  '#prefix' =&gt; '&lt;div id="trends-ajax-wrap"&gt;',
  '#suffix' =&gt; '&lt;div id="trends-visualization-container"&gt;&lt;/div&gt;&lt;/div&gt;',
  '#ajax' =&gt; array(
    'effect' =&gt; 'fade',
  ),
);

$form['trends-graph'] = array(
  '#prefix' =&gt; '&lt;div id="trends-graph",',
  '#suffix' =&gt; '&lt;/div&gt;',
);

$form['submit'] = array(
  '#type' =&gt; 'submit',
  '#value' =&gt; 'Submit',
  '#ajax' =&gt; array(
    'callback' =&gt; 'trends_ajax_submit_callback',
  )
);


The callback

function trends_ajax_submit_callback($ajax, &amp;$form, &amp;$form_state)  {

 $form_state['rebuild'] = TRUE;
 $commands = array();
 $commands[] = ajax_command_html('#trends-visualization-container',
   '&lt;script&gt;
     (function ($) {
       Drupal.behaviors.trends_ajax = {
         attach: function (context, settings) {
            postscribe(\'#trends-graph\', 
            \'&lt;script src="//www.google.com/trends/embed.js?hl=en-US&amp;q=Stephen%20Harper&amp;tz=Etc/GMT%2B4&amp;content=1&amp;cid=TIMESERIES_GRAPH_0&amp;export=5&amp;w=500&amp;h=330"&gt;
            &lt;/script&gt;\');
         }
       }
      })(jQuery);&lt;/script&gt;');

return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);


Triggering the Ajax with $form['submit'] results in the following JS error:

Uncaught SyntaxError: Unexpected token ILLEGAL


This is referencing my JS code, specifically this line:

postscribe('#trends-graph', '&lt;script src="//www.google.com/trends/embed.js?hl=en-US&amp;q=Stephen%20Harper&amp;tz=Etc/GMT%2B4&amp;content=1&amp;cid=TIMESERIES_GRAPH_0&amp;export=5&amp;w=500&amp;h=330"&gt;


Through googling it is suggested that there is an illegal hidden character somewhere, but after having looked at the code in VIM, EMACS, KATE, OKTETA, ATOM, JSBin, JSFiddle, and CodePen I am unable to find any strange hidden characters. The closest anomaly I can find are 0A characters through the hexeditor (Okteta), but I don't think those should be causing any problems.

Could someone provide some info as to how I can continue to troubleshoot?

Thanks a lot!
I managed to solve this by escaping one more character and removing one semicolon.

In the 2nd argument of ajax_command_html I was attaching a string which had two script tags, one inner and one outter. The inner closing script tag wasn't being passed unless I escaped its forward slash, like this:

&lt;\/script&gt;


Lastly, I had to remove the semicolon that was following "(jQuery)", just prior to closing the outter script tag, like this:

})(jQuery)&lt;/script&gt;');


Hopefully this can be of use to someone!