How do I add inline JavaScript to the footer? I want to add third-party JavaScript code at bottom of the every page. I build an module and I am able to add the JavaScript on the head tag, instead of footer.

I am using the following code.

function ur_optin_monster_preprocess_html(&amp;$variables) {
  $config = \Drupal::config('ur_form.admin_settings');
  $variables['#attached']['html_head'][] = [
    [
      '#tag' =&gt; 'script',
      '#value' =&gt; $config-&gt;get('my_js'),
      '#weight' =&gt; 400, 
    ],
    'ur_form_js',
  ];
}


I also tried changing 'html_head' to 'html_scripts', but it doesn't help. I tried with the batrik.theme file, but it gives the following error.


  Error: Call to undefined function drupal_add_js() in bartik_preprocess_html() (line 95 of core/themes/bartik/bartik.theme). 


function bartik_preprocess_html(&amp;$variables) {
  $config = \Drupal::config('ur_form.admin_settings');
  if ($config-&gt;get('my_js')) { 
    drupal_add_js($config-&gt;get('my_js'), array('scope'=&gt;'footer'));
  }
}


Can you suggest me the correct way to add JavaScript code at the footer of every page?
Until Add support for inline JS/CSS with #attached is resolved, you should add a JavaScript file normally as a library defined in a MYMODULE.libraries.yml and then pass your module's config as Drupal settings from PHP to this JavaScript file.

foobar:
  version: VERSION
  js:
    js/myscript.js: {}
  dependencies:
    - core/jquery
    - core/jquery.once
    - core/drupal
    - core/drupalSettings


Then in MYMODULE.module add the JS file and pass the config over to JS.

/**
 * Implements hook_page_attachments().
 */
function MYMODULE_page_attachments(array &amp;$attachments) {

  $attachments['#attached']['library'][] = 'MYMODULE/foobar';

  // Pass values from PHP to JS.
  $attachments['#attached']['drupalSettings']['MYMODULE']['lorem'] = \Drupal::config('MYMODULE.settings')-&gt;get('lorem');
  $attachments['#attached']['drupalSettings']['MYMODULE']['ipsum'] = \Drupal::config('MYMODULE.settings')-&gt;get('ipsum');
}


Then in the JS file access them like following.

(function($, Drupal) {
  'use strict';
  Drupal.behaviors.MyBehavior = {
    attach: function(context, settings) {

      $('html').once('MyBehavior').each(function() {

        var lorem = settings.MYMODULE.lorem;
        var ipsum = settings.MYMODULE.ipsum;

        // Do whatever you need to do.
        // ...

      });          
    }
  };
})(jQuery, Drupal);




I just found Inline JavaScript that is in an integration module in the docs. But it also says


  Using any inline JavaScript is highly discouraged. If you can use one of the examples above, please consider those before attempting to
  do this.