5

I want to prevent the user from refreshing the page, How to do this? I have been using e.preventDefault method but it`s not running properly.

RPichioli
  • 3,245
  • 2
  • 25
  • 29
Pramod Badgujar
  • 95
  • 1
  • 1
  • 5
  • 1
    Please share your work. – Rahul Patel Sep 09 '16 at 12:39
  • 1
    Effectively, you can't. Even if you can, you shouldn't. – Jonnix Sep 09 '16 at 12:39
  • You need to send `e` in your event properly, like this: `$(someElement).someAction(function(e){});` or use `return false` in the end of your method – GONG Sep 09 '16 at 12:41
  • Why do you want to break the user's browser functionality? – chris85 Sep 09 '16 at 12:41
  • @JonStirling thx for the reminder – Laith Sep 09 '16 at 12:47
  • this is the point of ajax : Update a web page without reloading the page . http://www.w3schools.com/ajax/ – Laith Sep 09 '16 at 12:48
  • Answer here: https://stackoverflow.com/q/3527041/632951 – Pacerier Oct 17 '17 at 09:49
  • The answers here (and on similar questions) all have the javascript `onbeforeunload` event to prevent someone from leaving a page. But what about ONLY nagging about a reload? Can I see where the browser is trying to go? And only alert the user if they are going to my site again? It's a dynamic status page, I don't want people constantly reloading when that's unnecessary. But I don't want to bug them if they are simply leaving. – l008com Nov 28 '18 at 09:09
  • I feel like scammers who scam the lives out of people by changing the page's HTML will use this to their advantage. – theknightD2 Feb 21 '21 at 22:57

4 Answers4

3

you can use the window.onbeforeunload even.

window.onbeforeunload = function() {
            return "you can not refresh the page";
        }
Mannan Bahelim
  • 1,289
  • 1
  • 11
  • 31
  • window.onbeforeunload = function(event) { event.returnValue = "Write something clever here.."; }; – Mannan Bahelim Jan 29 '18 at 10:39
  • 1
    Reason for not being supported : https://developers.google.com/web/updates/2016/04/chrome-51-deprecations?hl=en#remove-custom-messages-in-onbeforeload-dialogs – Mannan Bahelim Jan 29 '18 at 10:40
  • I tried this method window.onbeforeunload = function(event) { event.returnValue = "Write something clever here.."; }; But it is not working in chrome. – Humble_boy Jan 29 '18 at 10:55
2

The HTML specification states that authors should use the Event.preventDefault() method instead of using Event.returnValue to prompt the user.

window.addEventListener('beforeunload', function (e) {
  // Cancel the event
  e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
  // Chrome requires returnValue to be set
  e.returnValue = '';
});

Reference: WindowEventHandlers.onbeforeunload

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
1

Try something like:

<script type="text/javascript">
    // Callback
    window.onbeforeunload = function(e) {
        // Turning off the event
        e.preventDefault();
    }
</script>

Some basic explanations about these features

preventDefault: http://www.w3schools.com/jsref/event_preventdefault.asp

beforeunload: http://www.w3schools.com/jsref/event_onbeforeunload.asp

RPichioli
  • 3,245
  • 2
  • 25
  • 29
0

It is not advised, but if you want to completely prevent the user from refreshing the page in any way possible you can use:

setInterval(function(){
  window.location.reload();
  window.stop();
},100)

Not only will it prevent refreshing, but it will also prevent navigation.

EzWinz
  • 1
  • 1