Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.615
diff -u -d -F^\s*function -r1.615 common.inc
--- includes/common.inc	31 Jan 2007 15:49:22 -0000	1.615
+++ includes/common.inc	11 Feb 2007 20:10:51 -0000
@@ -1563,7 +1563,8 @@ function drupal_clear_css_cache() {
  *   Adds a reference to a JavaScript file to the page. JavaScript files
  *   are placed in a certain order, from 'core' first, to 'module' and finally
  *   'theme' so that files, that are added later, can override previously added
- *   files with ease.
+ *   files with ease. 'core' files in 'header' are only added to the page if
+ *   there are also other JavaScript files or code on that page.
  *
  * - Add inline JavaScript code ('inline'):
  *   Executes a piece of JavaScript code on the current page by placing the code
@@ -1604,28 +1605,25 @@ function drupal_clear_css_cache() {
  *   far for $scope is returned.
  */
 function drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE) {
-  if (!is_null($data)) {
-    _drupal_add_js('misc/jquery.js', 'core', 'header', FALSE, $cache);
-    _drupal_add_js('misc/drupal.js', 'core', 'header', FALSE, $cache);
-  }
-  return _drupal_add_js($data, $type, $scope, $defer, $cache);
-}
-
-/**
- * Helper function for drupal_add_js().
- */
-function _drupal_add_js($data, $type, $scope, $defer, $cache) {
-  static $javascript = array();
+  static $javascript = array(
+    'header' => array(
+      'core' => array(
+        'misc/jquery.js' => array('cache' => TRUE, 'defer' => FALSE),
+        'misc/drupal.js' => array('cache' => TRUE, 'defer' => FALSE),
+      ),
+      'module' => array(), 'theme' => array(), 'setting' => array(), 'inline' => array(),
+    ),
+  );
 
-  if (!isset($javascript[$scope])) {
-    $javascript[$scope] = array('core' => array(), 'module' => array(), 'theme' => array(), 'setting' => array(), 'inline' => array());
-  }
+  if (!is_null($data)) {
+    if (!isset($javascript[$scope])) {
+      $javascript[$scope] = array('core' => array(), 'module' => array(), 'theme' => array(), 'setting' => array(), 'inline' => array());
+    }
 
-  if (!isset($javascript[$scope][$type])) {
-    $javascript[$scope][$type] = array();
-  }
+    if (!isset($javascript[$scope][$type])) {
+      $javascript[$scope][$type] = array();
+    }
 
-  if (!is_null($data)) {
     switch ($type) {
       case 'setting':
         $javascript[$scope][$type][] = $data;
@@ -1638,7 +1636,7 @@ function _drupal_add_js($data, $type, $s
     }
   }
 
-  return $javascript[$scope];
+  return $javascript;
 }
 
 /**
@@ -1653,14 +1651,28 @@ function _drupal_add_js($data, $type, $s
  *   Defaults to 'header'.
  * @parameter $javascript
  *   (optional) An array with all JavaScript code. Defaults to the default
- *   JavaScript array for the given scope.
+ *   JavaScript array. This array should contain all scopes.
  * @return
  *   All JavaScript code segments and includes for the scope as HTML tags.
  */
 function drupal_get_js($scope = 'header', $javascript = NULL) {
   $output = '';
   if (is_null($javascript)) {
-    $javascript = drupal_add_js(NULL, NULL, $scope);
+    $javascript = drupal_add_js();
+  }
+
+  $count = 0;
+  foreach (array_keys($javascript) as $_scope) {
+    foreach (array_keys($javascript[$_scope]) as $_type) {
+      if ($_type != 'core' && !empty($javascript[$_scope][$_type])) $count++;
+    }
+  }
+
+  if (!isset($javascript[$scope]) || !$count) {
+    return '';
+  }
+  else {
+    $javascript = $javascript[$scope];
   }
 
   foreach ($javascript as $type => $data) {
