diff --git a/core/misc/ajax.es6.js b/core/misc/ajax.es6.js index daa62548a0..a2be21678f 100644 --- a/core/misc/ajax.es6.js +++ b/core/misc/ajax.es6.js @@ -953,7 +953,11 @@ const settings = this.settings || drupalSettings; Drupal.attachBehaviors(this.$form.get(0), settings); } - throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage); + // Do not throw an error if an ajax request has been interrupted due to + // leaving the page. + if (!(xmlhttprequest.status === 0 && Drupal.beforeUnloadCalled)) { + throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage); + } }; /** diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 1e15175edd..30dc719c44 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -463,7 +463,10 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr var settings = this.settings || drupalSettings; Drupal.attachBehaviors(this.$form.get(0), settings); } - throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage); + + if (!(xmlhttprequest.status === 0 && Drupal.beforeUnloadCalled)) { + throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage); + } }; Drupal.AjaxCommands = function () {}; diff --git a/core/misc/drupal.es6.js b/core/misc/drupal.es6.js index e64f9dac0d..60916d8e3f 100644 --- a/core/misc/drupal.es6.js +++ b/core/misc/drupal.es6.js @@ -43,6 +43,18 @@ window.Drupal = { behaviors: {}, locale: {} }; // JavaScript should be made compatible with libraries other than jQuery by // wrapping it in an anonymous closure. (function (Drupal, drupalSettings, drupalTranslations) { + + /** + * Add a global variable which determines if the window is being unloaded. + */ + Drupal.beforeUnloadCalled = false; + window.addEventListener('beforeunload', function () { + Drupal.beforeUnloadCalled = true; + }); + window.addEventListener('pagehide', function () { + Drupal.beforeUnloadCalled = true; + }); + /** * Helper to rethrow errors asynchronously. * diff --git a/core/misc/drupal.js b/core/misc/drupal.js index a00d8ce548..10f571ed10 100644 --- a/core/misc/drupal.js +++ b/core/misc/drupal.js @@ -8,6 +8,14 @@ window.Drupal = { behaviors: {}, locale: {} }; (function (Drupal, drupalSettings, drupalTranslations) { + Drupal.beforeUnloadCalled = false; + window.addEventListener('beforeunload', function () { + Drupal.beforeUnloadCalled = true; + }); + window.addEventListener('pagehide', function () { + Drupal.beforeUnloadCalled = true; + }); + Drupal.throwError = function (error) { setTimeout(function () { throw error;