Same name and namespace in other branches
  1. 5.x includes/common.inc \drupal_get_js()
  2. 7.x includes/common.inc \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.

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.

Return value

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

4 calls to drupal_get_js()
chameleon_page in themes/chameleon/chameleon.theme
template_preprocess_maintenance_page in includes/theme.maintenance.inc
The variables generated here is a mirror of template_preprocess_page(). This preprocessor will run it's course when theme_maintenance_page() is invoked. It is also used in theme_install_page() and theme_update_page() to keep all the variables…
template_preprocess_page in includes/theme.inc
Process variables for page.tpl.php
theme_closure in includes/theme.inc
Execute hook_footer() which is run at the end of the page right before the close of the body tag.

File

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

Code

function drupal_get_js($scope = 'header', $javascript = NULL) {
  if ((!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') && function_exists('locale_update_js_files')) {
    locale_update_js_files();
  }
  if (!isset($javascript)) {
    $javascript = drupal_add_js(NULL, NULL, $scope);
  }
  if (empty($javascript)) {
    return '';
  }
  $output = '';
  $preprocessed = '';
  $no_preprocess = array(
    'core' => '',
    'module' => '',
    'theme' => '',
  );
  $files = array();
  $preprocess_js = variable_get('preprocess_js', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update');
  $directory = file_directory_path();
  $is_writable = is_dir($directory) && is_writable($directory) && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC;

  // A dummy query-string is added to filenames, to gain control over
  // browser-caching. The string changes on every update or full cache
  // flush, forcing browsers to load a new copy of the files, as the
  // URL changed. Files that should not be cached (see drupal_add_js())
  // get time() as query-string instead, to enforce reload on every
  // page request.
  $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);

  // For inline Javascript to validate as XHTML, all Javascript containing
  // XHTML needs to be wrapped in CDATA. To make that backwards compatible
  // with HTML 4, we need to comment out the CDATA-tag.
  $embed_prefix = "\n<!--//--><![CDATA[//><!--\n";
  $embed_suffix = "\n//--><!]]>\n";
  foreach ($javascript as $type => $data) {
    if (!$data) {
      continue;
    }
    switch ($type) {
      case 'setting':
        $output .= '<script type="text/javascript">' . $embed_prefix . 'jQuery.extend(Drupal.settings, ' . drupal_to_js(call_user_func_array('array_merge_recursive', $data)) . ");" . $embed_suffix . "</script>\n";
        break;
      case 'inline':
        foreach ($data as $info) {
          $output .= '<script type="text/javascript"' . ($info['defer'] ? ' defer="defer"' : '') . '>' . $embed_prefix . $info['code'] . $embed_suffix . "</script>\n";
        }
        break;
      default:

        // If JS preprocessing is off, we still need to output the scripts.
        // Additionally, go through any remaining scripts if JS preprocessing is on and output the non-cached ones.
        foreach ($data as $path => $info) {
          if (!$info['preprocess'] || !$is_writable || !$preprocess_js) {
            $no_preprocess[$type] .= '<script type="text/javascript"' . ($info['defer'] ? ' defer="defer"' : '') . ' src="' . base_path() . $path . ($info['cache'] ? $query_string : '?' . time()) . "\"></script>\n";
          }
          else {
            $files[$path] = $info;
          }
        }
    }
  }

  // Aggregate any remaining JS files that haven't already been output.
  if ($is_writable && $preprocess_js && count($files) > 0) {

    // Prefix filename to prevent blocking by firewalls which reject files
    // starting with "ad*".
    $filename = 'js_' . md5(serialize($files) . $query_string) . '.js';
    $preprocess_file = drupal_build_js_cache($files, $filename);
    $preprocessed .= '<script type="text/javascript" src="' . base_path() . $preprocess_file . '"></script>' . "\n";
  }

  // Keep the order of JS files consistent as some are preprocessed and others are not.
  // Make sure any inline or JS setting variables appear last after libraries have loaded.
  $output = $preprocessed . implode('', $no_preprocess) . $output;
  return $output;
}