5

I've been asked to add the following to a page :

<script id="dsxc_script" type="text/javascript" src="https://www.docserver1.co.uk/dsxc/client.js?acc=xx&integrated=true"></script>

However I don't think the drupal_add_js() function will allow me to set an 'id' attribute.

How should I proceed ?

2
  • Seems Drupal does not have support for id built-in. If needed, hacking it directly into html.tpl.php is a way, byt it does not seem to be a really good way. – Mołot Oct 9 '13 at 11:30
  • Also replace & with &amp; in link. – user11153 Oct 9 '13 at 12:00
7

drupal_add_js does not support an ID attribute as it seems. However, as a workaround, you can use drupal_add_html_head to add stuff to the head section.

Ex:

$element = array(
  '#tag' => 'script',
  '#type' => 'html_tag',
  '#attributes' => array(
    'src' => 'https://www.docserver1.co.uk/dsxc/client.js?acc=xx&integrated=true',
    'type' => 'text/javascript',
    'id' => 'dsxc_script',
  ),
);
drupal_add_html_head($element, 'dsxc');


//dpm(drupal_render($element)); Gives the following:
// <script src="https://www.docserver1.co.uk/dsxc/client.js?acc=xx&amp;integrated=true" type="text/javascript" id="dsxc_script" />

You can always embed these scripts in your html.tpl.php file, but if you want to add the stuff conditionally, I think this will work.

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.