function drupal_get_js

You are here

7 drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALSE)
5 drupal_get_js($scope = 'header', $javascript = NULL)
6 drupal_get_js($scope = 'header', $javascript = NULL)
8 drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALSE, $is_ajax = 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 _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.


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

bool $skip_alter: (optional) If set to TRUE, this function skips calling \Drupal::moduleHandler->alter() on $javascript, useful when the calling function passes a $javascript array that has already been altered.

bool $is_ajax: (optional) If set to TRUE, this function is called from an Ajax request and adds javascript settings to update ajaxPageState values.

Return value

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

See also




25 calls to drupal_get_js()
AjaxResponse::ajaxRender in core/lib/Drupal/Core/Ajax/AjaxResponse.php
Prepares the AJAX commands for sending back to the client.
FrameworkTest::testLazyLoad in core/modules/system/lib/Drupal/system/Tests/Ajax/FrameworkTest.php
Tests that new JavaScript and CSS files are lazy-loaded on an AJAX request.
FrameworkTest::testOrder in core/modules/system/lib/Drupal/system/Tests/Ajax/FrameworkTest.php
Tests AjaxResponse::prepare() AJAX commands ordering.
JavaScriptTest::testAddJsFileWithQueryString in core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Tests JavaScript files that have querystrings attached get added right.
JavaScriptTest::testAggregatedAttributes in core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Tests that attributes are maintained when JS aggregation is enabled.

... See full list

1 string reference to 'drupal_get_js'
template_preprocess_html in core/includes/
Prepares variables for HTML document templates.


core/includes/, line 2063
Common functions that many Drupal modules will need to reference.


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

  // Allow modules to alter the JavaScript.
  if (!$skip_alter) {
    \Drupal::moduleHandler()->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.
    if (!empty($items['settings']) || $is_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 \Drupal\Core\Theme\AjaxBasePageNegotiator
      $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($javascript), 1);

      // 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 drupalSettings.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);