1

I need to prompt the user before leaving the edit page so that he doesn't lose unsaved changes. I've written the following code, but it is buggy. Once the user is on edit page and he declines the refresh operation after pressing F5, the normal refresh never triggers.

$(document.body).on("keydown", this, function (event) {
  if (event.keyCode == 116 && isEditPage()) {
    if (!confirm('you will lose all the pending changes. are you sure you want to leave this page?')) {
      return false;
    }
  }
  return true;
});
Rubyist
  • 6,486
  • 10
  • 51
  • 86
benjamin54
  • 1,280
  • 4
  • 28
  • 48

2 Answers2

0

You can register the event listener which will be triggered when user is leaving a page having unsaved changes. A page refresh can be initiated without even hitting f5 key (using browser navigation buttons, reload icon etc).

window.onbeforeunload = function (e) {
  var message = "Your confirmation message goes here.",
  e = e || window.event;
  // For IE and Firefox
  if (e) {
    e.returnValue = message;
  }

  // For Safari
  return message;
};
Rahul Malu
  • 556
  • 2
  • 9
0

please check this discussion and try this below way.

 $scope.$on('$routeChangeStart', function(next, current) { 
         return 'are you sure you want to leave?';
    });

You can get next and current route in the next, current params.

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234