简体   繁体   English

在 Safari 4 中禁用 F5 键

[英]Disable F5 key in Safari 4

I have written the following Javascript code to catch F5 key press and prevent the user from refreshing:我编写了以下 Javascript 代码来捕捉F5按键并防止用户刷新:

(I understand this is not a good idea but I am stuck with this whether any one likes it or not. Also the question I ask pertains to why the script below does not work with Safari 4 but works for other browsers.) (我知道这不是一个好主意,但无论有人喜欢与否,我都坚持这一点。另外,我问的问题是为什么下面的脚本不适用于 Safari 4 但适用于其他浏览器。)

var fn = function (e)
{

    if (!e)
        var e = window.event;

    var keycode = e.keyCode;
    if (e.which)
        keycode = e.which;

    var src = e.srcElement;
    if (e.target)
        src = e.target;    

    // 116 = F5
    if (116 == keycode)
    {
        // Firefox and other non IE browsers
        if (e.preventDefault)
        {
            e.preventDefault();
            e.stopPropagation();
        }
        // Internet Explorer
        else if (e.keyCode)
        {
            e.keyCode = 0;
            e.returnValue = false;
            e.cancelBubble = true;
        }

        return false;
    }
}

// Assign function to onkeydown event
document.onkeydown = fn;

The above code works perfectly for IE 6,7 and 8, from Firefox 2.0 onwards and also in Chrome.上面的代码非常适用于 IE 6,7 和 8,从 Firefox 2.0 开始,也适用于 Chrome。

However it does not work for Safari 4 for windows.但是它不适用于 Windows Safari 4。 Pressing the F5 key refreshes the document.F5键刷新文档。 The funny thing is that in Safari the code gets inside the if (e.preventDefault) part above but for some reason its not preventing the default action ie refreshing the page.有趣的是,在 Safari 中,代码位于上面的if (e.preventDefault)部分中,但由于某种原因,它并没有阻止默认操作,即刷新页面。

Is this a bug with Safari 4 or is there some Safari specific code I need to write?这是 Safari 4 的错误还是我需要编写一些特定于 Safari 的代码?

Never try anything that hinders a user's normal action with a browser.永远不要尝试任何妨碍用户使用浏览器进行正常操作的事情。 Even if you do this using javascript he can disable the script and then continue the page refresh.即使您使用 javascript 执行此操作,他也可以禁用脚本,然后继续刷新页面。

If you need to prevent the action made on a refresh then handle it in server side.如果您需要阻止在刷新时执行的操作,请在服务器端处理。

It seems like you are trying to obstruct the user from doing something that your code cannot handle easily, eg stopping them 'purchasing item X' twice by hitting refresh, or hitting your site too often by holding F5 to annoy you.似乎您试图阻止用户做一些您的代码无法轻松处理的事情,例如通过点击刷新来阻止他们“购买项目 X”两次,或者通过按住 F5 来惹恼您而频繁访问您的网站。

I'd suggest other solutions such as cookies/state management or blocking client IPs if you are preventing abuse.如果您要防止滥用,我会建议其他解决方案,例如 cookie/状态管理或阻止客户端 IP。

To the people who are saying, "don't do this": You aren't helping.对于那些说“不要这样做”的人:你没有帮助。 There is ALWAYS a side case where a rule of thumb must be broken.总有一个附带案例,必须打破经验法则。

For example, the client I'm working with has a system that has a major piece of their testing application built off of a second browser window, which goes full screen, no toolbars, bookmarks, or any other window options, and disables all keys that aren't directly affected by the testing environment.例如,我正在使用的客户端有一个系统,该系统的主要测试应用程序构建于第二个浏览器窗口,该窗口全屏显示,没有工具栏、书签或任何其他窗口选项,并禁用所有键不受测试环境的直接影响。 In this case, it is valuable to the user, who is likely someone of low technical skill, to be able to take the test without inadvertently hitting F5 and reloading a large, client-side series of questions with all of their previous answers erased.在这种情况下,对于可能是技术水平较低的用户而言,能够参加测试而不会无意中按 F5 并重新加载大量客户端系列问题并删除他们之前的所有答案,这对用户来说是有价值的。 The questions are client side because preloading allows the test to be run on a low-bandwidth connection, like those found in the third world.问题是客户端,因为预加载允许测试在低带宽连接上运行,就像在第三世界中发现的那样。

From what I can tell of this Safari-only issue (Chrome, another Webkit-based browser, doesn't have this problem), even the alert box will not keep the browser from refreshing.据我所知,这个仅限 Safari 的问题(Chrome,另一个基于 Webkit 的浏览器,没有这个问题),即使是警告框也不会阻止浏览器刷新。 The alert shows, then the page refreshes normally.警报显示,然后页面正常刷新。

Nothing I've tried seems to block the key.我试过的任何东西似乎都没有挡住钥匙。 I have read that this is considered a defect with Safari, and that a certain build number of Safari should contain a fix, but as of the time I'm writing this, this hasn't been implemented in the most recent version of Safari for Windows.我已经读到这被认为是 Safari 的一个缺陷,并且 Safari 的某个版本号应该包含一个修复程序,但是在我写这篇文章的时候,这还没有在最新版本的 Safari 中实现视窗。 I've tested this on the Mac, and it seems to be fixed on that platform.我已经在 Mac 上对此进行了测试,并且似乎在该平台上已修复。

+1 to everyone else saying “don't do this”. +1 其他人说“不要这样做”。 It's just not at all feasible.这根本不可行。 There are many other ways of refreshing the page... on my browser at least: the toolbar button*;还有许多其他刷新页面的方法……至少在我的浏览器上:工具栏按钮*; ctrl-R; Ctrl-R; right-click-reload;右键单击重新加载; ctrl-T to get a tab bar then right-click-on-tab-reload; ctrl-T 获取标签栏,然后右键单击标签重新加载; and so on.等等。 Tell your client it's impossible and get on with something actually useful.告诉你的客户这是不可能的,然后继续做一些真正有用的事情。

*: trying to hide the browser chrome is also not allowed by my browser, because it's a really bad idea. *:我的浏览器也不允许尝试隐藏浏览器 chrome,因为这是一个非常糟糕的主意。 You are opening yourself up to phishing attacks by eliding the essential indicators that tell the user that they are really on your site.通过省略告诉用户他们确实在您的网站上的基本指标,您正在向网络钓鱼攻击敞开大门。

Yes, it appears to be a bug with Safari 4 Windows.是的,它似乎是 Safari 4 Windows 的一个错误。 You cannot stop CTRL+F either.你也不能停止 CTRL+F。

However, opening an alert stops it, but that is a rough trade off.但是,打开警报会阻止它,但这是一个粗略的权衡。

I had a logic to handle the F5 similar to the question.I am displaying a confirm box which asks "Data you have entered may not be saved?".If the the user click Ok , page should reload and if click Cancel , page need to retain as it is.我有一个逻辑来处理类似于问题的 F5。我正在显示一个确认框,询问“您输入的数据可能不会被保存?”。如果用户点击Ok ,页面应该重新加载,如果点击Cancel ,页面需要保持原样。 But only in safari " Cancel " click also reload the page.但只有在 safari 中“取消”点击也会重新加载页面。 I have tested in Safari 5.1.7.我已经在 Safari 5.1.7 中进行了测试。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM