I want to disable browser refreshing using JavaScript.
Currently, I am using window.onbeforeunload
and I don't want it to be called when user refreshes the browser.
What is the best way to do it?
This question is related to
javascript
dom-events
From the site Enrique posted:
window.history.forward(1);
document.attachEvent("onkeydown", my_onkeydown_handler);
function my_onkeydown_handler() {
switch (event.keyCode) {
case 116 : // 'F5'
event.returnValue = false;
event.keyCode = 0;
window.status = "We have disabled F5";
break;
}
}
It works for me in all the browsers:
document.onkeydown = function(){
switch (event.keyCode){
case 116 : //F5 button
event.returnValue = false;
event.keyCode = 0;
return false;
case 82 : //R button
if (event.ctrlKey){
event.returnValue = false;
event.keyCode = 0;
return false;
}
}
}
$(window).bind('beforeunload', function(e) {
return "Unloading this page may lose data. What do you want to do..."
e.preventDefault();
});
This is the code I'm using to disable refresh on IE and firefox which works for the following key combinations:
F5 | Ctrl + F5 | Ctrl + R
//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";
}
}
}
If you don't want to use useragent to detect what type of browser it is ($.browser
uses navigator.userAgent
to determine the platform), you can use
if('MozBoxSizing' in document.documentElement.style)
which returns true for firefox
Use this for modern browsers:
function my_onkeydown_handler( event ) {
switch (event.keyCode) {
case 116 : // 'F5'
event.preventDefault();
event.keyCode = 0;
window.status = "F5 disabled";
break;
}
}
document.addEventListener("keydown", my_onkeydown_handler);
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)">
for mac cmd+r, cmd+shift+r to need.
function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
$(document).ready(function(){
$(document).on("keydown", disableF5);
});
var ctrlKeyDown = false;
$(document).ready(function(){
$(document).on("keydown", keydown);
$(document).on("keyup", keyup);
});
function keydown(e) {
if ((e.which || e.keyCode) == 116 || ((e.which || e.keyCode) == 82 && ctrlKeyDown)) {
// Pressing F5 or Ctrl+R
e.preventDefault();
} else if ((e.which || e.keyCode) == 17) {
// Pressing only Ctrl
ctrlKeyDown = true;
}
};
function keyup(e){
// Key up Ctrl
if ((e.which || e.keyCode) == 17)
ctrlKeyDown = false;
};
You can directly use hotkey from rich faces if you are using JSF.
<rich:hotKey key="backspace" onkeydown="if (event.keyCode == 8) return false;" handler="return false;" disableInInput="true" />
<rich:hotKey key="f5" onkeydown="if (event.keyCode == 116) return false;" handler="return false;" disableInInput="true" />
<rich:hotKey key="ctrl+R" onkeydown="if (event.keyCode == 123) return false;" handler="return false;" disableInInput="true" />
<rich:hotKey key="ctrl+f5" onkeydown="if (event.keyCode == 154) return false;" handler="return false;" disableInInput="true" />
Source: Stackoverflow.com