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

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

我不是 Drupal 专家而且 PHP.. 我想异步添加一个 js 文件,但我不知道如何添加。现在,当我向我的 Drupal 网站 (7.x) 添加一个 js 文件时,我使用

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

但现在我想添加一个异步的js文件。也许我应该添加

 async => TRUE

但我不知道在哪里....

所以请你能帮我看看如何重写 drupal_add_js 吗?

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

您可以使用 Drupal 7 的 "Asynchronous JavaScript" 模块做到这一点:https://www.drupal.org/project/async_js

或者您可以实施 hook_js_alter() 并将 async = TRUE 键添加到外部文件。在 Drupal 上检查 link:https://www.drupal.org/node/2299773

如果你想在你的头上添加一个外部 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');