18

I want to disable refresh on my website. Is there any way to disable F5, Ctrl-R, or reload in a webpage?

Naveed S
  • 5,106
  • 4
  • 34
  • 52
kiran
  • 281
  • 1
  • 3
  • 12
  • 4
    Every time I've heard a developer propose this possibility, it's because they've discovered that refreshing the browser breaks their code. But that's only really possible if their code ignores or overrides the basic navigation model of the Web browser -- the platform they're developing for. The answer is not to mess with the model even further. It's to fix whatever you're doing that screws up the normal, expected behavior of the Web. – Matt Howell Apr 19 '11 at 05:54
  • If the page contains any timer, need to disable the refresh. Else timer will start from the beginning. – Thaha kp Jan 29 '14 at 10:16
  • @Thahakp bigmattyh's comment suggests how to proceed: use sessionStorage/localStorage to store the timestamp when starting the timer, initialise the timer with its default start time minus the time elapsed, delete the stored value when the timer triggers. otherwise, imho, you accept to potentially impair user experience for the sake of relieving you from a programming inconvenience. – collapsar Apr 10 '14 at 07:45
  • 1
    @MattHowell It's not the case here, Reload does not break my code, it runs well and performs what I expect. But denying the reload by running a function instead would make user experience on my small application significantly better – Vladimir May 18 '18 at 18:17
  • @MattHowell There are scenarios such as uploading or server processing. or even temporary code editors that would benefit from such prompts. – A5H1Q Feb 02 '23 at 13:30

9 Answers9

24
window.onbeforeunload = function () {return false;}

This disables leaving the page unless the user confirms, not only refresh, so you'll have to code this yourself to get it to work.

You never said what you were doing with it so I left it at that.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • BBut how do you customize the message? – Pacerier Oct 17 '17 at 09:58
  • https://stackoverflow.com/questions/38879742/is-it-possible-to-display-a-custom-message-in-the-beforeunload-popup – Wesley Murch Oct 17 '17 at 21:01
  • "I'm surprised everyone says you can't do this" I think everyone is right. You can show a confirmation modal but you can't disable page reload no matter what. – Jaime Mar 07 '18 at 15:14
8

No, there is no way to disable it.

If you are trying to avoid the "Do you want to submit this action again?" dialog, use Post/Redirect/Get.

alex
  • 479,566
  • 201
  • 878
  • 984
3

Check for a cookie every time you enter the page. if the cookie is there then you know a refresh was made.

If the cookie is not there create one with zero duration (exist only till the browser still open)

Gideon
  • 31
  • 1
3

No, There is no such way. You can just warn user for not to refresh

jmj
  • 237,923
  • 42
  • 401
  • 438
3

I don't know how to disable refresh clicking (i still need that one myself) Other than using a server side script to stop the "redirect" HOWEVER, I do know of EXTREMELY easy jQuery to disable F5:

function disableF5(e) { if (e.which == 116) e.preventDefault(); };
// To disable f5
$(document).bind("keydown", disableF5);
// To re-enable f5
$(document).unbind("keydown", disableF5);
nfechner
  • 17,295
  • 7
  • 45
  • 64
SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
1

I have a suggestion. There is an answer box in this page.

(Here below with Your Answer label)

  • Type some text in it.
  • Then refresh this page

When you refresh there is some warning message displayed, I suggest you to display the similar warning message, instead of disabling the refresh functionality.

Renjith JR
  • 189
  • 1
  • 3
  • 14
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
0

There is a way to prevent the user from refreshing your page by pressing F5. Try this codes inside your body tag:

<body onkeydown="return (event.keyCode != 116)">
Ejardy
  • 101
  • 1
  • 4
  • 14
0

If you want to disable ctrl+f5 , ctrl+R , f5 ,backspace then you can use this simple code. This code is working in Mozila as well as Chrome . Add this code inside your body tag:

<body onkeydown="return (event.keyCode == 154)">
0

There may be some tricks that would work on some browsers, but there is nothing that could guarantee that they couldn't refresh the page. Browsers and web standards are wide open, so a user could easily hack up a browser to work around anything you implemented.

If refreshing would negatively impact the user experience, it is best to give them a textual warning.

If this is to fix a bug (such as a lack of idempotency), you should look into fixing the bug instead.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183