How to add a custom attribute to the script tag via drupal_add_js?

by kenorb   Last Updated January 12, 2018 13:07 PM

As part of API implementation of Queue-it framework, I need to add extra custom attribute data-queueit-c into script tag, e.g.

<script 
    data-queueit-c="MyClientID" 
    type="text/javascript" 
    src="//static.queue-it.net/script/queueconfigloader.min.js">
</script>

I'm aware you can add other attributes such as defer, however this one is custom.

I've tried the following code using drupal_add_js, but without luck:

drupal_add_js('//static.queue-it.net/script/queueconfigloader.min.js',
  [ 
    'type' => 'external',
    'data-queueit-c' => 'MyClientID',
  ]);

How this can be achieved?

Tags : 7 javascript


Answers 1


As for workaround, inline syntax can be used, e.g.

$js_code = "<script data-queueit-c='MyClientID' type='text/javascript' src='//static.queue-it.net/script/queueconfigloader.min.js'></script>";
drupal_add_js($js_code, 'inline');
kenorb
kenorb
January 12, 2018 12:41 PM

Related Questions


Use drupal.settings variables on javascript

Updated March 27, 2015 14:18 PM

Passing Javascript variable in my module file

Updated July 27, 2015 14:03 PM

Calling a contact form in a certain page

Updated August 23, 2015 17:03 PM