From 953faecd55db7b279035fdf6e617faa19c4020d7 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 21 May 2020 20:53:49 +0900 Subject: [PATCH] Detect AJAX abort due to page unload #1280 --- common/js/xml_handler.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/common/js/xml_handler.js b/common/js/xml_handler.js index e73063ac9..24207788a 100644 --- a/common/js/xml_handler.js +++ b/common/js/xml_handler.js @@ -15,6 +15,11 @@ */ window.show_leaving_warning = false; + /** + * This variable becomes true when the user tries to navigate away from the page. + */ + var page_unloading = false; + /** * This variable stores the .wfsr jQuery object. */ @@ -127,6 +132,11 @@ return xmlHandler(xhr, textStatus); } + // If the user is navigating away, don't do anything. + if (xhr.status == 0 && page_unloading) { + return; + } + // Hide the waiting message and display an error notice. clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); @@ -266,6 +276,13 @@ // Define the error handler. var errorHandler = function(xhr, textStatus) { + + // If the user is navigating away, don't do anything. + if (xhr.status == 0 && page_unloading) { + return; + } + + // Hide the waiting message and display an error notice. clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); var error_info; @@ -338,6 +355,13 @@ // Define the error handler. var errorHandler = function(xhr, textStatus) { + + // If the user is navigating away, don't do anything. + if (xhr.status == 0 && page_unloading) { + return; + } + + // Hide the waiting message and display an error notice. clearTimeout(wfsr_timeout); waiting_obj.hide().trigger("cancel_confirm"); var error_info = xhr.status + " " + xhr.statusText + " (" + textStatus + ")"; @@ -423,6 +447,7 @@ * Empty placeholder for beforeUnload handler. */ var beforeUnloadHandler = function() { + page_unloading = true; return ""; }; @@ -438,6 +463,10 @@ }).bind("ajaxStop cancel_confirm", function() { $(window).unbind("beforeunload", beforeUnloadHandler); }); + } else { + $(window).on('beforeunload', function() { + page_unloading = true; + }); } });