diff --git a/core/includes/ajax.inc b/core/includes/ajax.inc
index 8683ff9..c43d267 100644
--- a/core/includes/ajax.inc
+++ b/core/includes/ajax.inc
@@ -247,6 +247,10 @@ function ajax_render($commands = array()) {
       $function = 'drupal_add_' . $type;
       $items[$type] = $function();
       drupal_alter($type, $items[$type]);
+      if (isset($items['js']['settings'])) {
+        // Store the JS settings now so we don't have to alter them again.
+        $settings = $items['js']['settings'];
+      }
       // @todo Inline CSS and JS items are indexed numerically. These can't be
       //   reliably diffed with array_diff_key(), since the number can change
       //   due to factors unrelated to the inline content, so for now, we strip
@@ -289,9 +293,13 @@ 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'])) {
+  if (!isset($settings)) {
+    // We skipped altering above, do it here.
+    $scripts = drupal_add_js();
+    drupal_alter('js', $scripts);
     $settings = $scripts['settings'];
+  }
+  if (!empty($settings)) {
     array_unshift($commands, ajax_command_settings(call_user_func_array('array_merge_recursive', $settings['data']), TRUE));
   }
 
