1. some Drupal modules use hook_page_alter to add js to the $page['page_bottom'] region.

$page_bottom and $page['page_bottom'] are explained here: http://drupal.org/update/modules/6/7#hook_footer

2. I now want to change the scope of other js to be at the bottom of the page but at the top of $page['page_bottom'] so as to come before the inline js injected in 1.

I can't seem to do it.

in hook_js_alter, scope can be header, footer. $page_bottom, $page, $page['page_bottom'] are not available to hook_js_alter.

in hook_js_alter:

$script['scope'] = 'footer'; works
but 
$script['scope'] = $page_bottom; does not.

While group and weight work within hook_js_alter, they can't be used to alter the position vs. js added using hook_page_alter.

Comments

socialnicheguru’s picture

This works to place all the javascript at the bottom of the page in footer. I am concerned if a "footer" region is not defined does it default to $page['page_bottom']

function mytheme_js_alter(&$javascript) {
// Collect the scripts we want in to remain in the header scope.

$header_scripts = array(
drupal_get_path('module', 'drupalchat').'/js/drupalchat_external.js',
drupal_get_path('module', 'fb') . '/fb.js',
drupal_get_path('module', 'fb_invite') . '/fb_invite.js',
drupal_get_library('respondjs', 'respondjs.min.js'),
);

// Change the default scope of all other scripts to footer.
// We assume if the script is scoped to header it was done so by default.
foreach ($javascript as $key => &$script) {
if ($script['scope'] == 'header' && !in_array($script['data'], $header_scripts)) {
$script['scope'] = 'footer';
// $script['scope'] = $page_bottom; this does not work when I place global $page_bottom at the top of function
}
}

// Change the weight of $javascript to appear at the top of the
// footer or page_bottom region
// Modules like fb and drupalchat inject js into the page_bottom region
// if page_bottom is not defined it is injected into footer region
// Insert $javascript before the js declarations made by modules

// This does not work
$javascript['settings']['group'] = -500;
$javascript['settings']['weight'] = -100;

if(module_exists('devel')){
dpm($javascript);
}

}

Version: 7.21 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.