How to prevent user to press F5 and CTRL+R in Mozilla Firefox 24 using javascript or jquery.
Asked
Active
Viewed 755 times
-1
-
This post might help you: http://stackoverflow.com/questions/2878983/capture-key-press-without-placing-an-input-element-on-the-page – Moduo Feb 27 '15 at 08:31
-
I really think its a bad idea to restrict the user. Instead, consider using sessions to store the variable values that cant change. Then you can just restore the state of the page. Between the user frustration and the developer frustration and false sense of accomplishment/security, this is a bad path to head down. – Jul 24 '18 at 17:39
2 Answers
0
You can't really prevent the user from reloading your page, but you can show up a message before leaving or reloading:
window.onbeforeunload = function(e) {
return 'Do You really want to leave the page?';
};
You can also set up an event handler for Ctrl or F5 keypress, and prevent defaults from happening, but it will not work for function keys (like Ctrl or F keys) in most browsers, for security reasons.

Balázs Varga
- 1,797
- 2
- 16
- 32
0
<script language="javascript" type="text/javascript">
//this code handles the F5/Ctrl+F5/Ctrl+R
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;
// Mozilla firefox
if ($.browser.mozilla) {
if (keycode == 116 ||(e.ctrlKey && keycode == 82)) {
if (e.preventDefault)
{
e.preventDefault();
e.stopPropagation();
}
}
}
// IE
else if ($.browser.msie) {
if (keycode == 116 || (window.event.ctrlKey && keycode == 82)) {
window.event.returnValue = false;
window.event.keyCode = 0;
window.status = "Refresh is disabled";
}
}
}
</script>

Kevin
- 415
- 3
- 8
- 21