0

How can i add a script file with type='module', like this :

<script src="site/test/test/index.js" type="module"></script>

I tried with :

  drupal_add_js(drupal_get_path('module', 'my_module') . '/assets/js/my_script.js', [
    'attributes' => ['type' => 'module'],
    'type' => 'module',
  ]);

But Drupal 7 only accept 'file', 'inline', 'external' or 'setting' as type.

Do you have any idea ?

Thanks.

4
  • You cannot, for the reason you said. What would you expect to happen, when the type is module?
    – apaderno
    Aug 29, 2019 at 8:47
  • Hello, i want to insert a ReactJs script and i need to set the type at module to import a dependencie in my script. Aug 29, 2019 at 8:57
  • Just use one of the values the function accepts basing on the script being used. Drupal doesn't recognize a ReactJS module as script type.
    – apaderno
    Aug 29, 2019 at 8:59
  • This one can help you link Aug 29, 2019 at 9:44

1 Answer 1

0

You can do this in hook_preprocess_html():

function theme_preprocess_html(&$var) {
  $script = [
    '#tag' => 'script',
    '#attributes' => [
      'type' => 'module',
      'src' => 'path/to/script.js',
    ],
    '#value' => '',
  ];
  drupal_add_html_head($script, 'script');  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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