diff --git a/uniqueness.module b/uniqueness.module
index bb8c248..ac43a6a 100644
--- a/uniqueness.module
+++ b/uniqueness.module
@@ -139,25 +139,25 @@ function uniqueness_form_alter(&$form, $form_state, $form_id) {
     _uniqueness_widget_store(array('type' => $type, 'op' => $op)); // Save the content type and operation for later use
     if (user_access('use uniqueness widget') && in_array($op, variable_get('uniqueness_type_' . $type, array()))) {
       // Add our javascript.
-      _uniqueness_add_search_javascript($type, $form['nid']['#value']);
-
+      $form['uniqueness']['#attached']['js'] = array(
+        drupal_get_path('module', 'uniqueness') . '/uniqueness.js',
+        array(
+          'data' => array('uniqueness' => _uniqueness_get_js_settings($type, $form['nid']['#value'], !empty($form_state['node_preview']))),
+          'type' => 'setting',
+        )
+      );
       // Embed inline widget if enabled.
       if (uniqueness_widget_enabled(UNIQUENESS_WIDGET_INLINE)) {
         // Only add the inline uniqueness widget if it has not been defined
         // already. This makes it possible for other modules to customize the
         // form element in their own hook_form_alter() implementation.
-        if (!isset($form['uniqueness'])) {
-          $form['uniqueness'] = array();
-        }
-        if (!isset($form['uniqueness']['uniqueness_type'])) {
-          $form['uniqueness']['uniqueness_type'] = array();
-        }
         $form['uniqueness'] += array(
           '#type' => 'fieldset',
           '#title' => filter_xss_admin(variable_get('uniqueness_default_title', t('Related content'))),
           '#collapsible' => 1,
           '#collapsed' => 1,
           '#weight' => $form['title']['#weight'] + 1, // Place underneath title.
+          'uniqueness_type' => array(),
         );
         $form['uniqueness']['uniqueness_type'] += array(
           '#type' => 'item',
@@ -225,33 +225,28 @@ function _uniqueness_widget_store($options = NULL) {
 }
 
 /**
- * Generates and embeds javascript code required for the uniqueness search.
+ * Returns the jQuery settings required for the uniqueness search.
  */
-function _uniqueness_add_search_javascript($type, $nid) {
-  drupal_add_js(drupal_get_path('module', 'uniqueness') . '/uniqueness.js');
-  $search_url = base_path() . 'uniqueness-search/' . $type;
-  $search_mode = variable_get('uniqueness_search_mode', UNIQUENESS_SEARCH_MODE_NODETITLE);
+function _uniqueness_get_js_settings($type, $nid, $preview) {
   $minCharacters = variable_get('uniqueness_query_min', 3);
-  if ($search_mode == UNIQUENESS_SEARCH_MODE_DRUPAL && $minCharacters < variable_get('minimum_word_size', 3)) {
+  if (variable_get('uniqueness_search_mode', UNIQUENESS_SEARCH_MODE_NODETITLE) == UNIQUENESS_SEARCH_MODE_DRUPAL
+      && $minCharacters < variable_get('minimum_word_size', 3)) {
     $minCharacters = variable_get('minimum_word_size', 3);
   }
   $settings = array(
-    'URL' => $search_url,
-    'preview' => FALSE,
+    'URL' => base_path() . 'uniqueness-search/' . $type,
+    'preview' => $preview,
     'prependResults' => variable_get('uniqueness_results_prepend', 0) == 1 ? TRUE : FALSE,
     'minCharacters' => $minCharacters,
     'searchingString' => filter_xss_admin(variable_get('uniqueness_searching_string', t('Searching&hellip;'))),
   );
-  if (!empty($form_state['node_preview'])) {
-    $settings['preview'] = TRUE;
-  }
   if (variable_get('uniqueness_scope', UNIQUENESS_SCOPE_CONTENT_TYPE) == UNIQUENESS_SCOPE_CONTENT_TYPE) {
     $settings['type'] = $type;
   }
   if (is_numeric($nid) && $nid != 0) {
     $settings['nid'] = $nid;
   }
-  drupal_add_js(array('uniqueness' => $settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
+  return $settings;
 }
 
 /**
