4

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.

2 Answers 2

4

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');
Sign up to request clarification or add additional context in comments.

Comments

3

You could do that with the "Asynchronous JavaScript" module for Drupal 7 at : https://www.drupal.org/project/async_js

Or you could implement hook_js_alter() and add in the async = TRUE key to the external file. Check this link on Drupal: https://www.drupal.org/node/2299773

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.