diff --git a/core/includes/ajax.inc b/core/includes/ajax.inc
index 3f2389f..b6273e1 100644
--- a/core/includes/ajax.inc
+++ b/core/includes/ajax.inc
@@ -248,6 +248,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
@@ -290,9 +294,13 @@ function ajax_render($commands = array()) {
   }
 
   // Now add a command to merge changes and additions to drupalSettings.
-  $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 = drupal_merge_js_settings($scripts['settings']['data']);
+  }
+  if (!empty($settings)) {
     array_unshift($commands, ajax_command_settings($settings, TRUE));
   }
 
