Code for how to prevent user from refreshing the page either by using refresh button of browser or CTRL+F5 in angular js
Asked
Active
Viewed 3,136 times
-2
-
You can't. Why do you want to prevent it? – Aleksey Solovey Jun 13 '18 at 13:01
-
Because that page is holding form data and user may accidentally refresh the page – Raja Singh Jun 13 '18 at 13:05
-
save it in a `localstorage` – Aleksey Solovey Jun 13 '18 at 13:06
-
Are you asking how to make the "please confirm you want to leave the page" dialogå like the one you get when you try to refresh when writing an answer? – Haem Jun 13 '18 at 13:27
1 Answers
1
Well you cannot stop the user form manually refreshing the page, and not a good UX as well. Instead you can ask for confirmation for page reload. In JS we handle this using window.onbeforeunload:
window.onbeforeunload = function() {
return "Your form progress will be lost, are you sure you want to reload this page?";
}

Vishu
- 338
- 1
- 10
-
Note that newer browsers [don't support a custom onBeforeUnload message](https://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own). – Haem Jun 13 '18 at 13:34