async => TRUE in drupal_add_js() :对 js 文件的异步调用


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

我不是Drupal和PHP专家。我想异步添加一个 js 文件,但我不知道怎么做。现在,当我将js文件添加到我的Drupal网站(7.x)时,我使用

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

但是现在我想添加一个异步的JS文件。也许我应该补充一下

 async => TRUE

但我不知道在哪里...

所以请你帮我,展示如何重写drupal_add_js

我希望你能帮助我,非常感谢,对不起我的英语。

如果你想

在脑袋中添加一个外部javascript,你可以使用这样的drupal_add_html_head

  $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');
你可以

使用 Drupal 7 的 "Asynchronous JavaScript" 模块在 : https://www.drupal.org/project/async_js

或者你可以实现 hook_js_alter() 并将 async = TRUE 键添加到外部文件中。在Drupal上查看此链接:https://www.drupal.org/node/2299773