From b06d30c0f92af5514b4b259fec00226e6e5670ab Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 18 Oct 2017 09:29:50 +0200 Subject: [PATCH] U4-10554 - cross-browser --- .../validation/valformmanager.directive.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/directives/validation/valformmanager.directive.js b/src/Umbraco.Web.UI.Client/src/common/directives/validation/valformmanager.directive.js index ab22894a21..a8c3261924 100644 --- a/src/Umbraco.Web.UI.Client/src/common/directives/validation/valformmanager.directive.js +++ b/src/Umbraco.Web.UI.Client/src/common/directives/validation/valformmanager.directive.js @@ -117,19 +117,27 @@ function valFormManager(serverValidationManager, $rootScope, $log, $timeout, not }); unsubscribe.push(locationEvent); - var savedUnloadHandler = $window.onbeforeunload; - $window.onbeforeunload = function () { + // try to do it in the most cross-browser way + // some browsers will display their own custom message + // and some will just ignore this completely + function onBeforeUnload(e) { if (formCtrl.$dirty) { - return "You have unsaved changes."; // most browsers won't display it anyways + var message = "You have unsaved changes."; + e.returnValue = message; + return message; + } else { + return null; } } + $window.addEventListener("beforeunload", onBeforeUnload); + //Ensure to remove the event handler when this instance is destroyted scope.$on('$destroy', function() { for (var u in unsubscribe) { unsubscribe[u](); } - $window.onbeforeunload = savedUnloadHandler; + $window.removeEventListener("beforeunload", onBeforeUnload); }); $timeout(function(){