function drupal_get_js

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 drupal_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 drupal_js_defaults(). See locale_js_alter() for an example of this.

Parameters

$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 drupal_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

drupal_add_js()

locale_js_alter()

drupal_js_defaults()

23 calls to drupal_get_js()
AjaxResponse::ajaxRender in drupal/core/lib/Drupal/Core/Ajax/AjaxResponse.php
Prepares the AJAX commands for sending back to the client.
ajax_render in drupal/core/includes/ajax.inc
Renders a commands array into JSON.
FrameworkTest::testLazyLoad in drupal/core/modules/system/lib/Drupal/system/Tests/Ajax/FrameworkTest.php
Tests that new JavaScript and CSS files are returned on an Ajax request.
JavaScriptTest::testAggregatedAttributes in drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Tests that attributes are maintained when JS aggregation is enabled.
JavaScriptTest::testAggregation in drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Tests JavaScript grouping and aggregation.

... See full list

File

drupal/core/includes/common.inc, line 3790
Common functions that many Drupal modules will need to reference.

Code

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

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

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

    // Sort the JavaScript files so that they appear in the correct order.
    uasort($items, 'drupal_sort_css_js');

    // Don't add settings if there is no other JavaScript on the page, unless
    // this is an AJAX request.
    // @todo Clean up container call.
    $container = drupal_container();
    if ($container
      ->has('content_negotiation') && $container
      ->isScopeActive('request')) {
      $type = $container
        ->get('content_negotiation')
        ->getContentType($container
        ->get('request'));
    }
    if (!empty($items['settings']) || !empty($type) && $type == 'ajax') {
      global $theme_key;

      // Provide the page with information about the theme that's used, so that
      // a later AJAX request can be rendered using the same theme.
      // @see ajax_base_page_theme()
      $setting['ajaxPageState']['theme'] = $theme_key;

      // Checks that the DB is available before filling theme_token.
      if (!defined('MAINTENANCE_MODE')) {
        $setting['ajaxPageState']['theme_token'] = drupal_get_token($theme_key);
      }

      // 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);
      unset($setting['ajaxPageState']['js']['settings']);

      // Provide the page with information about the individual CSS files used,
      // information not otherwise available when CSS aggregation is enabled.
      // The setting is attached later in this function, but is set here, so
      // that CSS files removed in drupal_process_attached() are still
      // considered "used" and prevented from being added in a later AJAX
      // request.
      // Skip if no files were added to the page otherwise jQuery.extend() will
      // overwrite the Drupal.settings.ajaxPageState.css object with an empty
      // array.
      $css = drupal_add_css();
      if (!empty($css)) {

        // Cast the array to an object to be on the safe side even if not empty.
        $setting['ajaxPageState']['css'] = (object) array_fill_keys(array_keys($css), 1);
      }
      drupal_add_js($setting, 'setting');

      // If we're outputting the header scope, then this might be the final time
      // that drupal_get_js() is running, so add the settings to this output as well
      // as to the drupal_add_js() cache. If $items['settings'] doesn't exist, it's
      // because drupal_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 drupal_render($elements);
}