JS ordering in Drupal

Adding .js in info files adds that js file to every page of the website.
Scripts added in a theme's .info file are added at the theme level of ordering and will come after core/library JavaScript and module JavaScript.
Ordring: core modules => contributed modules => theme 

Altering the scope of the JS file

function YOURTHEME_js_alter(&$javascript) {
  $header_scripts = array(
    'sites/all/libraries/modernizr/modernizr.min.js',
    'misc/drupal.js',
    'sites/all/modules/jquery_update/replace/jquery/1.5/jquery.min.js',
  );

  foreach ($javascript as $key => &$script) {
    if ($script['scope'] == 'header' && !in_array($script['data'], $header_scripts)) {
      $script['scope'] = 'footer';
    }
  } 
}

Comments

Popular posts from this blog

Programatically create layout builder section in Drupal

Code quality analysis of Drupal. Can I use Sonar?

Set up Drupal7 to Drupal8 migration in simple steps (using Drush)