I've spent quite some time on drupal_add_js and didn't manage to solve this. How can I load an external JavaScript script which will also have the defer attribute?

I want to use this type of syntax:

drupal_add_js('http://example.com/whatever.js', 'external');

But it only produces:

<script src="http://example.com/whatever.js"></script>

I want the end HTML to be:

<script defer="defer" src="http://example.com/whatever.js"></script>

2 Answers 2


Try the following syntax:

drupal_add_js('http://example.com/whatever.js', array('type' => 'external', 'defer' => 'defer'));

Just add a value in $options

$options = array(
  'type' => 'external',
  'defer' => TRUE,
drupal_add_js('http://example.com/whatever.js', $options);

From the documentation:


$options: (optional) A string defining the type of JavaScript that is being added in the $data parameter ('file'/'setting'/'inline'/'external'), or an associative array. JavaScript settings should always pass the string 'setting' only. Other types can have the following elements in the array:

  • type: The type of JavaScript that is to be added to the page. Allowed values are 'file', 'inline', 'external' or 'setting'. Defaults to 'file'.

  • ...

  • defer: If set to TRUE, the defer attribute is set on the tag. Defaults to FALSE.

  • Good answer, found it interesting the drupal 7 still doesn't support async attribute, drupal.org/project/drupal/issues/1664602
    – powpow12
    Jul 22, 2020 at 14:19
  • @powpow12 Drupal 7 is fading out so no one focusing on any new feature....
    – Jimmy Ko
    Jul 23, 2020 at 13:13
  • 1
    Covid19 blew the door open with how long d7 will be around till, right now slated for 11/28/2022.
    – powpow12
    Jul 23, 2020 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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