Skip to content

Instantly share code, notes, and snippets.

@pascalduez
Created December 1, 2011 16:47
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save pascalduez/1418121 to your computer and use it in GitHub Desktop.
Save pascalduez/1418121 to your computer and use it in GitHub Desktop.
Drupal 7 — Move $scripts at page bottom
<!DOCTYPE html>
<html<?php print $html_attributes; ?>>
<head>
<?php print $head; ?>
<title><?php print $head_title; ?></title>
<?php print $styles; ?>
<?php print $head_scripts; ?>
</head>
<body<?php print $body_attributes;?>>
<?php print $page_top; ?>
<?php print $page; ?>
<?php print $scripts; ?>
<?php print $page_bottom; ?>
</body>
</html>
<?php
// Used in conjunction with https://gist.github.com/1417914
/**
* Implements hook_preprocess_html().
*/
function THEMENAME_preprocess_html(&$vars) {
// Move JS files "$scripts" to page bottom for perfs/logic.
// Add JS files that *needs* to be loaded in the head in a new "$head_scripts" scope.
// For instance the Modernizr lib.
$path = drupal_get_path('theme', 'THEMENAME');
drupal_add_js($path . '/js/modernizr.min.js', array('scope' => 'head_scripts', 'weight' => -1, 'preprocess' => FALSE));
}
/**
* Implements hook_process_html().
*/
function THEMENAME_process_html(&$vars) {
$vars['head_scripts'] = drupal_get_js('head_scripts');
}
@BarisW
Copy link

BarisW commented Mar 8, 2013

Awesome snippet, thanks for sharing!

@Skerth
Copy link

Skerth commented May 5, 2017

How to do the same, only for CSS? I could not do it using the functions: drupal_get_css, drupal_add_css

Drupal_add_css does not have the "scope" attribute

@matinfo
Copy link

matinfo commented Apr 5, 2018

Notice: for page rendering point of view is not recommended to put CSS in the bottom the page, only JS (except library like Modernizr and of course GA or GTM or other Tag Manager).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment