0

How to stop users to go back,refresh and without saving the current page by using angular 11.Any suggestions greatly appreciated.

I have tried the following script, but it's doesn't disable the back button,refresh and submit.

Thanks and Regards.

history.push State(null, null, window.location.href);

this.Location.onPopState(() => {

history.pushState(null, null, window.location.href);

this.stepper.previous();

BALARAMA
  • 11
  • 5
  • Does this answer your question? [How can I stop the browser back button using JavaScript?](https://stackoverflow.com/questions/12381563/how-can-i-stop-the-browser-back-button-using-javascript) and/or [How to disable the back button in the browser using JavaScript](https://stackoverflow.com/questions/19926641/how-to-disable-the-back-button-in-the-browser-using-javascript) – Luuk Dec 10 '21 at 10:55
  • Also note this [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting). Your code has an unequal number of `{` characters compared to the number of `}` characters. (and is not formatted properly...) – Luuk Dec 10 '21 at 11:00

1 Answers1

0

You cannot really disable the browser's back button just like you cannot stop the user from closing the window/tab and re-open the previous page. However you can display a warning before leaving the page using the onbeforeunload-event:

window.addEventListener('beforeunload', (event) => {
  if (thereAreChanges) {
    event.returnValue = 'Please save your changes before leaving this page';
  }
});