drupal_get_js

Definition

drupal_get_js($scope = 'header', $javascript = NULL)
includes/common.inc, line 1672

Description

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.

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.

Related topics

Namesort iconDescription
Input validationFunctions to validate user input.

Code

<?php
function drupal_get_js($scope = 'header', $javascript = NULL) {
  $output = '';
  if (is_null($javascript)) {
    $javascript = drupal_add_js(NULL, NULL, $scope);
  }

  foreach ($javascript as $type => $data) {
    if (!$data) continue;

    switch ($type) {
      case 'setting':
        $output .= '<script type="text/javascript">Drupal.extend({ settings: '. drupal_to_js(call_user_func_array('array_merge_recursive', $data)) ." });</script>\n";
        break;
      case 'inline':
        foreach ($data as $info) {
          $output .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .'>'. $info['code'] ."</script>\n";
        }
        break;
      default:
        foreach ($data as $path => $info) {
          $output .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. check_url(base_path() . $path) . ($info['cache'] ? '' : '?'. time()) ."\"></script>\n";
        }
    }
  }

  return $output;
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.