简体   繁体   English

使用 JavaScript 禁用 F5 和浏览器刷新

[英]Disable F5 and browser refresh using JavaScript

I want to disable browser refreshing using JavaScript.我想使用 JavaScript 禁用浏览器刷新。

Currently, I am using window.onbeforeunload and I don't want it to be called when user refreshes the browser.目前,我正在使用window.onbeforeunload并且我不希望在用户刷新浏览器时调用它。

What is the best way to do it?最好的方法是什么?

Update A 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.附带说明:这只会禁用键盘上的 f5 按钮。 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.不能说我真的知道如何做到这一点,因为我还没有这样做。

On the software site that I work at, we use my disableF5 function in conjunction with Codeigniter's session data.在我工作的软件站点上,我们将 disableF5 函数与 Codeigniter 的会话数据结合使用。 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.功能“disableF5”既快捷又简单,可防止该按钮执行任何操作。 However, to prevent the mouse-click on refresh button, a couple things take place.然而,为了防止鼠标点击刷新按钮,会发生一些事情。

  1. When lock is clicked, user session data has a variable called "locked" that becomes TRUE单击锁定时,用户会话数据有一个名为“已锁定”的变量,该变量变为 TRUE
  2. 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,则我们简单地不允许重定向并且页面永远不会更改,无论请求的目的地如何

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.提示:尝试使用服务器设置的 cookie,例如 PHP 的$_SESSION ,甚至 .Net 的Response.Cookies ,以保持客户端在站点中的“位置”。 This is the more Vanilla way to do what I do with CI's Session class.这是比较香草办法做我与CI的Session类做。 The big difference being that CI uses a Table in your DB, whereas these vanilla methods store an editable cookie in the client.最大的不同之处在于CI使用表中您的数据库,而这些香草方法保存在客户端可编辑的cookie。 The downside though, is a user can clear its cookies.但缺点是用户可以清除其 cookie。

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

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

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

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);
$(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:这是我用来在 IE 和 firefox 上禁用刷新的代码,它适用于以下组合键:

F5 | F5 | Ctrl + F5 | Ctrl + F5 | Ctrl + R 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如果不想使用useragent来检测浏览器是什么类型的( $.browser使用navigator.userAgent来判断平台),可以使用

if('MozBoxSizing' in document.documentElement.style) which returns true for firefox if('MozBoxSizing' in document.documentElement.style)为 firefox 返回 true

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

Enter this simple script in pure JS:在纯 JS 中输入这个简单的脚本:

document.addEventListener('keydown', (e) => {
    e = e || window.event;
    if(e.keyCode == 116){
        e.preventDefault();
    }
});

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

If you want to disable ctrl+f5 , ctrl+R , f5 ,backspace then you can use this simple code.如果你想禁用 ctrl+f5 , ctrl+R , f5 , backspace 那么你可以使用这个简单的代码。 This code is working in Mozila as well as Chrome .此代码适用于 Mozila 和 Chrome 。 Add this code inside your body tag:将此代码添加到您的 body 标签中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在浏览器上禁用F5刷新? - How do I disable the F5 refresh on the browser? 关闭浏览器时显示警告框,但不显示页面刷新或使用f5或浏览器刷新按钮刷新 - Show alert box when closing browser, but not on page reload or refresh using f5 or browser refresh button 通过javascript / jquery限制浏览器刷新,f5,像银行网站一样返回浏览器 - Restrict browser refresh, f5, browser back like banking sites through javascript/jquery javascript在刷新页面后保留隐藏输入变量的值(F5或单击浏览器的刷新按钮) - javascript retaining the value of hidden input variables even after refreshing page ( F5 or clicking refresh button of browser) 如何区分刷新(通过 F5/刷新按钮)和关闭浏览器? - How to distinguish between refresh (by F5/refresh button) and close browser? 当用户尝试使用f5,ctrl + 5使用jquery脚本刷新浏览器页面时,警报无效 - Alert is not working when user try to refresh browser page using f5,ctrl+5 using jquery script 按F5或刷新按钮时强制重新加载浏览器 - Force reload the browser when pressing F5 or refresh button 知道浏览器刷新或按下 F5 的方法 - Way to know browser refresh or F5 has been hit 在Silverlight中禁用F5 - Disable F5 in Silverlight 使用JavaScript阻止F5键 - Block F5 key using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM