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.


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');

Your Answer

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