使用 JavaScript 禁用 F5 和浏览器刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2482059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable F5 and browser refresh using JavaScript
提问by Salil
I want to disable browser refreshing using JavaScript.
我想使用 JavaScript 禁用浏览器刷新。
Currently, I am using window.onbeforeunloadand I don't want it to be called when user refreshes the browser.
目前,我正在使用window.onbeforeunload并且不希望在用户刷新浏览器时调用它。
What is the best way to do it?
最好的方法是什么?
回答by SpYk3HH
UpdateA recent comment claims this doesn't work in the new Chrome ... As shown in jsFiddle, and tested on my personal site, this method still works as of Chrome ver 26.0.1410.64 m
更新最近的评论声称这在新的 Chrome 中不起作用......如 jsFiddle 所示,并在我的个人网站上进行了测试,此方法从 Chrome 版本开始仍然有效26.0.1410.64 m
This is REALLY easy in jQuery by the way:
顺便说一下,这在 jQuery 中非常简单:
jsFiddle
js小提琴
// slight update to account for browsers not supporting e.which
function disableF5(e) { if ((e.which || e.keyCode) == 116) e.preventDefault(); };
// To disable f5
/* jQuery < 1.7 */
$(document).bind("keydown", disableF5);
/* OR jQuery >= 1.7 */
$(document).on("keydown", disableF5);
// To re-enable f5
/* jQuery < 1.7 */
$(document).unbind("keydown", disableF5);
/* OR jQuery >= 1.7 */
$(document).off("keydown", disableF5);
On a side note: This only disables the f5 button on the keyboard. To truly disable refresh you must use a server side script to check for page state changes. Can't say I really know how to do this as I haven't done it yet.
附带说明:这只会禁用键盘上的 f5 按钮。要真正禁用刷新,您必须使用服务器端脚本来检查页面状态更改。不能说我真的知道该怎么做,因为我还没有这样做。
On the software site that I work at, we use my disableF5 function in conjunction with Codeigniter's session data. For instance, there is a lock button which will lock the screen and prompt a password dialog. The function "disableF5" is quick and easy and keeps that button from doing anything. However, to prevent the mouse-click on refresh button, a couple things take place.
在我工作的软件站点上,我们将 disableF5 函数与 Codeigniter 的会话数据结合使用。例如,有一个锁定按钮可以锁定屏幕并提示密码对话框。功能“disableF5”既快捷又简单,可防止该按钮执行任何操作。然而,为了防止鼠标点击刷新按钮,会发生一些事情。
- When lock is clicked, user session data has a variable called "locked" that becomes TRUE
- When the refresh button is clicked, on the master page load method is a check against session data for "locked", if TRUE, then we simple don't allow the redirect and the page never changes, regardless of requested destination
- 单击锁定时,用户会话数据有一个名为“已锁定”的变量,该变量变为 TRUE
- 单击刷新按钮时,在母版页面加载方法上检查会话数据是否为“锁定”,如果为 TRUE,则我们简单地不允许重定向并且页面永远不会更改,无论请求的目的地如何
TIP:Try using a server-set cookie, such as PHP's $_SESSION, or even .Net's Response.Cookies, to maintain "where" your client is in your site. This is the more Vanillaway to do what I do with CI's Session class. The big difference being that CI uses a Table in your DB, whereas these vanillamethods store an editable cookie in the client. The downside though, is a user can clear its cookies.
提示:尝试使用服务器设置的 cookie,例如 PHP 的$_SESSION,甚至 .Net 的Response.Cookies,来维护您的客户端在您的站点中的“位置”。这是比较香草办法做我与CI的Session类做。最大的不同之处在于CI使用表中您的数据库,而这些香草方法保存在客户端可编辑的cookie。但缺点是用户可以清除其 cookie。
回答by kzh
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;
}
}
回答by gokhan
for mac cmd+r, cmd+shift+r to need.
mac cmd+r, cmd+shift+r 需要。
function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
$(document).ready(function(){
$(document).on("keydown", disableF5);
});
回答by Jos
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;
};
回答by Hla Min Swe
$(window).bind('beforeunload', function(e) {
return "Unloading this page may lose data. What do you want to do..."
e.preventDefault();
});
回答by Saurav S.
This is the code I'm using to disable refresh on IE and firefox (This works well for F5, Ctr+F5 and Ctrl+R)
这是我用来在 IE 和 firefox 上禁用刷新的代码(这适用于 F5、Ctr+F5 和 Ctrl+R)
<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>
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
如果不想使用useragent来检测浏览器是什么类型的($.browser使用navigator.userAgent来判断平台),可以使用
if('MozBoxSizing' in document.documentElement.style)- returns true for firefox
if('MozBoxSizing' in document.documentElement.style)- 为 Firefox 返回 true
回答by Orhun Alp Oral
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);
回答by pratik
You can directly use hotkey from rich faces if you are using JSF.
如果您使用的是 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" />
回答by Simran Jeet Singh Bagga
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:
如果你想禁用 ctrl+f5 , ctrl+R , f5 , backspace 那么你可以使用这个简单的代码。此代码适用于 Mozila 和 Chrome 。将此代码添加到您的 body 标签中:
<body onkeydown="return (event.keyCode == 154)">
回答by Albert Maristany
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;
}
}
}

