diff --git a/core/includes/common.inc b/core/includes/common.inc
index 353a9b5..fb90881 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -4105,16 +4105,9 @@ function drupal_add_js($data = NULL, $options = NULL) {
     // Add jquery.js and drupal.js, as well as the basePath setting, the
     // first time a JavaScript file is added.
     if (empty($javascript)) {
-      // url() generates the prefix using hook_url_outbound_alter(). Instead of
-      // running the hook_url_outbound_alter() again here, extract the prefix
-      // from url().
-      url('', array('prefix' => &$prefix));
       $javascript = array(
         'settings' => array(
-          'data' => array(
-            array('basePath' => base_path()),
-            array('pathPrefix' => empty($prefix) ? '' : $prefix),
-          ),
+          'data' => array(),
           'type' => 'setting',
           'scope' => 'header',
           'group' => JS_SETTING,
@@ -4122,23 +4115,7 @@ function drupal_add_js($data = NULL, $options = NULL) {
           'weight' => 0,
           'browsers' => array(),
         ),
-        'core/misc/drupal.js' => array(
-          'data' => 'core/misc/drupal.js',
-          'type' => 'file',
-          'scope' => 'header',
-          'group' => JS_LIBRARY,
-          'every_page' => TRUE,
-          'weight' => -1,
-          'preprocess' => TRUE,
-          'cache' => TRUE,
-          'defer' => FALSE,
-          'browsers' => array(),
-        ),
-      );
-      // Register all required libraries.
-      drupal_add_library('system', 'jquery', TRUE);
-      drupal_add_library('system', 'jquery.once', TRUE);
-      drupal_add_library('system', 'html5shiv', TRUE);
+      );  
     }
 
     switch ($options['type']) {
@@ -4226,12 +4203,68 @@ function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALS
   if (empty($javascript)) {
     return '';
   }
-
+  
+  // @TODO: test if this can work on without respecting $scope
   // Allow modules to alter the JavaScript.
   if (!$skip_alter) {
     drupal_alter('js', $javascript);
   }
+  
+  // Decide if we need to output something
+  // No custom settings and only one entry
+  if (empty($javascript['settings']['data']) && count($javascript) == 1) {
+    return '';
+  }
+  // No custom settings and only one entry for ajaxPageState 
+  if (count($javascript['settings']['data']) == 1 && count($javascript) == 1) {
+    if (count($javascript['settings']['data'][0]) == 1) {
+      if (isset($javascript['settings']['data'][0]['ajaxPageState']) && count($javascript['settings']['data'][0]['ajaxPageState']) == 1) {
+        return '';
+      }
+    }
+  }
 
+  // Register all required libraries.
+  static $libraries_added = FALSE;
+  if (!$libraries_added) {
+    $libraries_added = TRUE;
+    drupal_add_library('system', 'jquery', TRUE);
+    drupal_add_library('system', 'jquery.once', TRUE);
+    drupal_add_library('system', 'html5shiv', TRUE);
+  }
+
+  // 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()
+  global $theme_key;
+  $setting['ajaxPageState'] = array(
+    'theme' => $theme_key,
+    'theme_token' => drupal_get_token($theme_key),
+  );
+  drupal_add_js($setting, 'setting');
+  
+  // @TODO: find a way to add it only once
+  // url() generates the prefix using hook_url_outbound_alter(). Instead of
+  // running the hook_url_outbound_alter() again here, extract the prefix
+  // from url().
+  url('', array('prefix' => &$prefix));
+  $javascript['settings']['data'] += array(
+    array('basePath' => base_path()),
+    array('pathPrefix' => empty($prefix) ? '' : $prefix),
+  );
+  $javascript['core/misc/drupal.js'] = array(
+    'data' => 'core/misc/drupal.js',
+    'type' => 'file',
+    'scope' => 'header',
+    'group' => JS_LIBRARY,
+    'every_page' => TRUE,
+    'weight' => -1,
+    'preprocess' => TRUE,
+    'cache' => TRUE,
+    'defer' => FALSE,
+    'browsers' => array(),
+  );  
+  
   // Filter out elements of the given scope.
   $items = array();
   foreach ($javascript as $key => $item) {
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 99e872e..8731446 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -101,15 +101,6 @@ function drupal_theme_initialize() {
 
   // Themes can have alter functions, so reset the drupal_alter() cache.
   drupal_static_reset('drupal_alter');
-
-  // 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'] = array(
-    'theme' => $theme_key,
-    'theme_token' => drupal_get_token($theme_key),
-  );
-  drupal_add_js($setting, 'setting');
 }
 
 /**
