0

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?

1

It's old but I have the same problem (adding "crossorigin" attribute) and this is my answer :

  • There is an issue about it here : https://www.drupal.org/project/drupal/issues/1664602

  • @kenorb solution can't work because "drupal_add_js" add "script" tag automatically. But... you can try this workaround (a little dirty) : close the script tag and open yours without closing

.

drupal_add_js('//--><!]]></script><script src="//static.queue-it.net/script/queueconfigloader.min.js" data-queueit-c="MyClientID"><!--//--><![CDATA[//><!--', 'inline');
-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');
  • 2
    The alternative looks like a real pain. drupal_get_js is very selective about what attributes it will use, so you'd need to re-implement that whole function and at the same time convince the system to use your new version instead. Not worth the hassle IMO – Clive Jan 12 '18 at 12:47
  • This does not seem to work, here is what i get : <script type="text/javascript"> <!--//--><![CDATA[//><!-- <script data-queueit-c='MyClientID' type='text/javascript' src='//static.queue-it.net/script/queueconfigloader.min.js'></script> //--><!]]> </script> – Thony Jul 2 '19 at 9:31
  • @Thony This solution is for Drupal 7, maybe you're using 8. – kenorb Jul 2 '19 at 10:39
  • No I am using 7 – Thony Jul 3 '19 at 9:34

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.