1

It's not really clear how to add custom attributes when adding an external JS script to the page.

Currently I have this code:

drupal_add_js('https://code.jquery.com/jquery-3.4.1.js', array('type' => 'external'));

How can I add an integrity attribute to the rendered script tag?

The rendered script should look like this:

<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="></script>

I know this post exists, but I'm not able to use that solution since the source is loaded externally and I will not be able to check on the source.

0

1 Answer 1

2

I solved the problem using drupal_add_html like this,

$data = array(
  '#tag' => 'script',
  '#value' => '',
  '#attributes' => array(
    'src' => 'https://code.jquery.com/jquery-3.4.1.js',
    'integrity' => 'sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=',
  ),
);
drupal_add_html_head($data, 'unique_key');
1
  • How to even use drupal_add_html ? is this a module which needs to be installed or what ??? Jun 1, 2022 at 11:23

Your Answer

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