diff --git a/core/includes/ajax.inc b/core/includes/ajax.inc
index 8817356..ac8ae1a 100644
--- a/core/includes/ajax.inc
+++ b/core/includes/ajax.inc
@@ -289,9 +289,9 @@ function ajax_render($commands = array()) {
   }
 
   // Now add a command to merge changes and additions to Drupal.settings.
-  $scripts = drupal_add_js();
-  if (!empty($scripts['settings'])) {
-    $settings = $scripts['settings'];
+  $javascript_settings = drupal_add_js_settings();
+  if (!empty($javascript_settings['settings'])) {
+    $settings = $javascript_settings['settings'];
     array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $settings['data']), TRUE));
   }
 
diff --git a/core/includes/common.inc b/core/includes/common.inc
index 353a9b5..7fb2e7a 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -4102,65 +4102,84 @@ function drupal_add_js($data = NULL, $options = NULL) {
   $options['weight'] += count($javascript) / 1000;
 
   if (isset($data)) {
-    // 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),
-          ),
-          'type' => 'setting',
-          'scope' => 'header',
-          'group' => JS_SETTING,
-          'every_page' => TRUE,
-          '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);
+    if ($options['type'] == 'setting') {
+      drupal_add_js_settings($data);
     }
+    else {
+      // Add jquery.js and drupal.js the first time a JavaScript file is added.
+      if (empty($javascript)) {
+        $javascript = 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']) {
-      case 'setting':
-        // All JavaScript settings are placed in the header of the page with
-        // the library weight so that inline scripts appear afterwards.
-        $javascript['settings']['data'][] = $data;
-        break;
-
-      case 'inline':
-        $javascript[] = $options;
-        break;
+      switch ($options['type']) {
+        case 'inline':
+          $javascript[] = $options;
+          break;
 
-      default: // 'file' and 'external'
-        // Local and external files must keep their name as the associative key
-        // so the same JavaScript file is not added twice.
-        $javascript[$options['data']] = $options;
+        default: // 'file' and 'external'
+          // Local and external files must keep their name as the associative key
+          // so the same JavaScript file is not added twice.
+          $javascript[$options['data']] = $options;
+      }
     }
   }
+
+  if (!empty($javascript)) {
+    $js_settings = drupal_add_js_settings();
+    $javascript['settings'] =  $js_settings['settings'];
+  }
+
   return $javascript;
 }
 
+function drupal_add_js_settings($data = NULL) {
+  $js_settings = &drupal_static(__FUNCTION__, array());
+
+  // All JavaScript settings are placed in the header of the page with
+  // the library weight so that inline scripts appear afterwards.
+  if (empty($js_settings)) {
+    // 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));
+    $js_settings['settings'] = array(
+      'data' => array(
+        array('basePath' => base_path()),
+        array('pathPrefix' => empty($prefix) ? '' : $prefix),
+      ),
+      'type' => 'setting',
+      'scope' => 'header',
+      'group' => JS_SETTING,
+      'every_page' => TRUE,
+      'weight' => 0,
+      'browsers' => array(),
+    );
+  }
+
+  if (isset($data)) {
+    $js_settings['settings']['data'][] = $data;
+  }
+
+  return $js_settings;
+}
+
 /**
  * Constructs an array of the defaults that are used for JavaScript items.
  *
@@ -4226,7 +4245,6 @@ function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALS
   if (empty($javascript)) {
     return '';
   }
-
   // Allow modules to alter the JavaScript.
   if (!$skip_alter) {
     drupal_alter('js', $javascript);
diff --git a/core/modules/system/tests/common.test b/core/modules/system/tests/common.test
index f0ac957..e384011 100644
--- a/core/modules/system/tests/common.test
+++ b/core/modules/system/tests/common.test
@@ -1203,6 +1203,7 @@ class CommonJavaScriptTestCase extends DrupalWebTestCase {
     // Reset drupal_add_js() and drupal_add_library() statics before each test.
     drupal_static_reset('drupal_add_js');
     drupal_static_reset('drupal_add_library');
+    drupal_static_reset('drupal_add_js_settings');
   }
 
   function tearDown() {
@@ -1238,7 +1239,8 @@ class CommonJavaScriptTestCase extends DrupalWebTestCase {
    * Test adding settings.
    */
   function testAddSetting() {
-    $javascript = drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting');
+    drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting');
+    $javascript = drupal_add_js_settings();
     $this->assertEqual(280342800, $javascript['settings']['data'][2]['dries'], t('JavaScript setting is set correctly.'));
     $this->assertEqual('rocks', $javascript['settings']['data'][2]['drupal'], t('The other JavaScript setting is set correctly.'));
   }
@@ -1266,6 +1268,7 @@ class CommonJavaScriptTestCase extends DrupalWebTestCase {
     // Only the second of these two entries should appear in Drupal.settings.
     drupal_add_js(array('commonTestArray' => array('key' => 'commonTestOldValue')), 'setting');
     drupal_add_js(array('commonTestArray' => array('key' => 'commonTestNewValue')), 'setting');
+    drupal_add_js('core/misc/jquery.js');
 
     $javascript = drupal_get_js('header');
     $this->assertTrue(strpos($javascript, 'basePath') > 0, t('Rendered JavaScript header returns basePath setting.'));
@@ -1627,6 +1630,16 @@ class CommonJavaScriptTestCase extends DrupalWebTestCase {
     $query_string = variable_get('css_js_query_string', '0');
     $this->assertRaw(drupal_get_path('module', 'node') . '/node.js?' . $query_string, t('Query string was appended correctly to js.'));
   }
+
+  /**
+   * Tests that Drupal.settings aren't added when not needed.
+   */
+  function testPageWithoutJavascript() {
+    $this->drupalGet('');
+    $content = $this->drupalGetContent();
+    $this->assertTrue(strpos($content, 'Drupal.settings') === FALSE, t('Drupal.settings is not added to the page.'));
+    $this->assertTrue(strpos($content, '.js') === FALSE, t('No javascript is added to the page.'));
+  }
 }
 
 /**
