1 common.inc backdrop_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALSE)

Returns a themed presentation of all JavaScript code for the current page.

References to JavaScript files are placed in a certain order: first, all 'core' files, then all 'module' and finally all 'theme' JavaScript files are added to the page. Then, all settings are output, followed by 'inline' JavaScript code. If running update.php, all preprocessing is disabled.

Note that hook_js_alter(&$javascript) is called during this function call to allow alterations of the JavaScript during its presentation. Calls to backdrop_add_js() from hook_js_alter() will not be added to the output presentation. The correct way to add JavaScript during hook_js_alter() is to add another element to the $javascript array, deriving from backdrop_js_defaults(). See locale_js_alter() for an example of this.


$scope: (optional) The scope for which the JavaScript rules should be returned. Defaults to 'header'.

$javascript: (optional) An array with all JavaScript code. Defaults to the default JavaScript array for the given scope.

$skip_alter: (optional) If set to TRUE, this function skips calling backdrop_alter() on $javascript, useful when the calling function passes a $javascript array that has already been altered.

Return value

All JavaScript code segments and includes for the scope as HTML tags.:

See also





core/includes/common.inc, line 4844
Common functions that many Backdrop modules will need to reference.


function backdrop_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALSE) {
  if (!isset($javascript)) {
    $javascript = backdrop_add_js();
  if (empty($javascript)) {
    return '';

  // Allow modules to alter the JavaScript.
  if (!$skip_alter) {
    backdrop_alter('js', $javascript);

  // Filter out elements of the given scope.
  $items = array();
  foreach ($javascript as $key => $item) {
    if ($item['scope'] == $scope) {
      $items[$key] = $item;

  // Fix 'every_page_weight' in case it is missing.
  foreach ($items as $key => &$item) {
    if (!isset($item['every_page_weight'])) {
      $item['every_page_weight'] = $item['every_page'] ? -1 : 1;

  // Sort the JavaScript so that it appears in the correct order.
  backdrop_sort($items, array('group', 'every_page_weight', 'weight'));

  // Provide the page with information about the individual JavaScript files
  // used, information not otherwise available when aggregation is enabled.
  $setting['ajaxPageState']['js'] = array_fill_keys(array_keys($items), 1);
  backdrop_add_js($setting, 'setting');

  // If we're outputting the header scope, then this might be the final time
  // that backdrop_get_js() is running, so add the setting to this output as well
  // as to the backdrop_add_js() cache. If $items['settings'] doesn't exist, it's
  // because backdrop_get_js() was intentionally passed a $javascript argument
  // stripped of settings, potentially in order to override how settings get
  // output, so in this case, do not add the setting to this output.
  if ($scope == 'header' && isset($items['settings'])) {
    $items['settings']['data'][] = $setting;

  // Render the HTML needed to load the JavaScript.
  $elements = array(
    '#type' => 'scripts',
    '#items' => $items,

  return backdrop_render($elements);