kiran
kiran

Reputation: 301

Can I disable browser refresh in my webapp?

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

Upvotes: 20

Views: 75391

Answers (9)

No Results Found
No Results Found

Reputation: 102745

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.

Upvotes: 24

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)">

Upvotes: 0

Ejardy
Ejardy

Reputation: 101

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)">

Upvotes: 0

Waqas Raja
Waqas Raja

Reputation: 10872

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.

Upvotes: 1

SpYk3HH
SpYk3HH

Reputation: 22570

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);

Upvotes: 3

Gideon
Gideon

Reputation: 31

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)

Upvotes: 3

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

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.

Upvotes: 0

alex
alex

Reputation: 490253

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.

Upvotes: 8

Jigar Joshi
Jigar Joshi

Reputation: 240900

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

Upvotes: 3

Related Questions