1

How can I use drupal_add_js() to include the following script?

<script src="http://example.net/script.js" data-config="{'skin':'skins/black/skin.css','volume':50,'autoplay':true,'shuffle':true,'repeat':1,'placement':'bottom','showplaylist':false,'playlist':[{'title':'','url':'https://soundcloud.com/fosterthepeoplemusic/pumpedupkicks'}]}" ></script>
1

I recently came across this issue and was able to work around the limitations of drupal_add_js() with the following hack:

//this can be any string of js that's already wrapped in <script> elements
$my_script = '<script ...[all the things]... ></script>';

drupal_add_js('</script>' . $my_script . '<script>', array(
  'type' => 'inline',
  'scope' => 'header', //or 'footer'
  //...any other options you want to add
));

You end up with two empty <script> elements in your markup, which I find annoying, but they don't actually do any harm.

0

You can do the same with drupal_add_html_head like below:

$data_config = "{'skin':'skins/black/skin.css','volume':50,'autoplay':true,'shuffle':true,'repeat':1,'placement':'bottom','showplaylist':false,'playlist':[{'title':'','url':'https://soundcloud.com/fosterthepeoplemusic/pumpedupkicks'}]}";
$my_script = array(
    '#tag' => 'script',
    '#attributes' => array(
        'src' => 'http://example.net/script.js',
        'type' => 'text/javascript',
        'data-config' => $data_config,
    )
);
drupal_add_html_head($my_script, 'mymodule');
4
  • Thanks, but this code put the js after head tag. but how can insert it right under <body> or <body>
    – Abraham
    Mar 31 '16 at 1:36
  • is it possible to use just drupal_add_js() instead of drupal_add_html_head?
    – Abraham
    Mar 31 '16 at 2:57
  • Unfortunately we cannot use drupal_add_js(). See drupal.org/node/1664602
    – Vergil K
    Mar 31 '16 at 15:22
  • Also drupal_add_html_head() adds output to the HEAD tag of the HTML page. We cannot make it insert in the <body> tag. You would have to do that manually somewhere in the page using a template. i.e. if you REALLY need this in the <body>.
    – Vergil K
    Mar 31 '16 at 15:25
0

AdvAgg can do it as it supports the attributes key.

drupal_add_js("http://example.net/script.js", array(
  'attributes' => array(
    'data-config' => "{'skin':'skins/black/skin.css','volume':50,'autoplay':true,'shuffle':true,'repeat':1,'placement':'bottom','showplaylist':false,'playlist':[{'title':'','url':'https://soundcloud.com/fosterthepeoplemusic/pumpedupkicks'}]}",
  ),
));

If you want to target a specific location for the js, advagg also allows for this; on "admin/config/development/performance/advagg" check the "Allow for JS to be added to blocks and views" button at the bottom.

drupal_add_js("http://example.net/script.js", array(
  'scope' => 'page_top:prefix',
  'attributes' => array(
    'data-config' => "{'skin':'skins/black/skin.css','volume':50,'autoplay':true,'shuffle':true,'repeat':1,'placement':'bottom','showplaylist':false,'playlist':[{'title':'','url':'https://soundcloud.com/fosterthepeoplemusic/pumpedupkicks'}]}",
  ),
));

Set $conf['theme_debug'] = TRUE; in your settings.php file to see all possible scopes; simply look for <!-- AdvAgg page_top:prefix tag --> in the page source code and know the JS will land there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.