Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async => TRUE in drupal_add_js() : asynchronous call for a js file

I'm not an expert Drupal and PHP.. I would like to add a js file asynchronous but i don't know how. Now when I add a js file to my Drupal website (7.x) i use

drupal_add_js(path_to_theme() . '/scripts/Home.js');

but now i would like to add a js file asynchronous. Maybe i should to add

 async => TRUE

but i don't know where....

So please can you help me and show how rewrite drupal_add_js ?

I hope you can help me, thanks a lot and sorry for my english.

like image 510
Borja Avatar asked Oct 29 '25 15:10

Borja


1 Answers

If you want to add an external javascript to your head you can use drupal_add_html_head like this

  $element = [
    '#tag' => 'script',
    '#value' => '',
    '#attributes' => [
      'src' => 'https://www.example.com/example.js',
      'type' => 'application/javascript',
      'async' => "async",
    ],
  ];
  drupal_add_html_head($element, 'my_async_js');
like image 65
fabianfiorotto Avatar answered Oct 31 '25 07:10

fabianfiorotto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!