diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 490ac45..f568767 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -41,6 +41,16 @@ define('ERROR_REPORTING_DISPLAY_SOME', 1); define('ERROR_REPORTING_DISPLAY_ALL', 2); /** + * JS error reporting level: display errors in console. + */ +define('JS_ERROR_REPORTING_CONSOLE', 0); + +/** + * JS error reporting level: display errors in an alert window. + */ +define('JS_ERROR_REPORTING_ALERT', 1); + +/** * Indicates that the item should never be removed unless explicitly selected. * * The item may be removed using cache_clear_all() with a cache ID. diff --git a/includes/common.inc b/includes/common.inc index c97704c..690d4fb 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -4271,6 +4271,7 @@ function drupal_add_js($data = NULL, $options = NULL) { 'data' => array( array('basePath' => base_path()), array('pathPrefix' => empty($prefix) ? '' : $prefix), + array('alertDisplay' => (int) variable_get('js_error_level', JS_ERROR_REPORTING_ALERT)), ), 'type' => 'setting', 'scope' => 'header', diff --git a/index.php b/index.php index 8b83199..e9007fb 100644 --- a/index.php +++ b/index.php @@ -19,3 +19,6 @@ define('DRUPAL_ROOT', getcwd()); require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); menu_execute_active_handler(); + +$javascript = drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting'); +print_r($javascript); diff --git a/misc/ajax.js b/misc/ajax.js index bb4a6e1..c7b3dc6 100644 --- a/misc/ajax.js +++ b/misc/ajax.js @@ -230,16 +230,16 @@ Drupal.ajax = function (base, element, element_settings) { * will test to see if the key press is valid to trigger this event and * if it is, trigger it for us and prevent other keypresses from triggering. * In this case we're handling RETURN and SPACEBAR keypresses (event codes 13 - * and 32. RETURN is often used to submit a form when in a textfield, and - * SPACE is often used to activate an element without submitting. + * and 32. RETURN is often used to submit a form when in a textfield, and + * SPACE is often used to activate an element without submitting. */ Drupal.ajax.prototype.keypressResponse = function (element, event) { // Create a synonym for this to reduce code confusion. var ajax = this; // Detect enter key and space bar and allow the standard response for them, - // except for form elements of type 'text' and 'textarea', where the - // spacebar activation causes inappropriate activation if #ajax['keypress'] is + // except for form elements of type 'text' and 'textarea', where the + // spacebar activation causes inappropriate activation if #ajax['keypress'] is // TRUE. On a text-type widget a space should always be a space. if (event.which == 13 || (event.which == 32 && element.type != 'text' && element.type != 'textarea')) { $(ajax.element_settings.element).trigger(ajax.element_settings.event); @@ -476,7 +476,8 @@ Drupal.ajax.prototype.getEffect = function (response) { * Handler for the form redirection error. */ Drupal.ajax.prototype.error = function (xmlhttprequest, uri, customMessage) { - alert(Drupal.ajaxError(xmlhttprequest, uri, customMessage)); + Drupal.alert(Drupal.ajaxError(xmlhttprequest, uri, customMessage)); + // Remove the progress element. if (this.progress.element) { $(this.progress.element).remove(); diff --git a/misc/autocomplete.js b/misc/autocomplete.js index d71441b..2b68cb6 100644 --- a/misc/autocomplete.js +++ b/misc/autocomplete.js @@ -310,7 +310,7 @@ Drupal.ACDB.prototype.search = function (searchString) { } }, error: function (xmlhttp) { - alert(Drupal.ajaxError(xmlhttp, db.uri)); + Drupal.alert(Drupal.ajaxError(xmlhttp, db.uri)); } }); }, this.delay); diff --git a/misc/drupal.js b/misc/drupal.js index 427c4a1..db3ee8c 100644 --- a/misc/drupal.js +++ b/misc/drupal.js @@ -414,6 +414,20 @@ Drupal.getSelection = function (element) { }; /** + * Shows an error on an alert window or the console + */ + Drupal.alert = function (message) { + if (Drupal.settings.alertDisplay) { + alert(message); + } + else { + if (typeof(window.console) !== 'undefined') { + window.console.log(message); + } + } + } + +/** * Build an error message from an Ajax response. */ Drupal.ajaxError = function (xmlhttp, uri, customMessage) { diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 92aefe4..49a4e3e 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -1420,8 +1420,8 @@ class JavaScriptTestCase extends DrupalWebTestCase { */ function testAddSetting() { $javascript = drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting'); - $this->assertEqual(280342800, $javascript['settings']['data'][2]['dries'], 'JavaScript setting is set correctly.'); - $this->assertEqual('rocks', $javascript['settings']['data'][2]['drupal'], 'The other JavaScript setting is set correctly.'); + $this->assertEqual(280342800, $javascript['settings']['data'][3]['dries'], 'JavaScript setting is set correctly.'); + $this->assertEqual('rocks', $javascript['settings']['data'][3]['drupal'], 'The other JavaScript setting is set correctly.'); } /** diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc index 8ef7d7c..e90b9f0 100644 --- a/modules/system/system.admin.inc +++ b/modules/system/system.admin.inc @@ -1676,6 +1676,17 @@ function system_logging_settings() { '#description' => t('It is recommended that sites running on production environments do not display any errors.'), ); + $form['js_error_level'] = array( + '#type' => 'radios', + '#title' => t('Javascript alert behaviour'), + '#default_value' => variable_get('js_error_level', JS_ERROR_REPORTING_ALERT), + '#options' => array( + JS_ERROR_REPORTING_ALERT => t('Show in alert window'), + JS_ERROR_REPORTING_CONSOLE => t('Show in console'), + ), + '#description' => t('This applies to certain javascript errors (i.e. AJAX), allowing you to disable the alert and have them being logged to the browser console when available.'), + ); + return system_settings_form($form); }