在Safari 4中禁用F5键 [英] Disable F5 key in Safari 4

查看:89
本文介绍了在Safari 4中禁用F5键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了以下javascript代码来捕捉F5键按下并阻止用户刷新:

(我明白这不是一个好主意但是我坚持这个是否有人喜欢它我问的问题也与下面的脚本不适用于safari 4但适用于其他浏览器的原因有关。)

I have written the following javascript code to catch F5 key press and prevent the user from refreshing:
(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.)

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;

以上代码适用于IE 6,7和8,从Firefox 2.0开始以及在Chrome中运行。
但它不适用于Safari 4 for Windows。按F5键刷新文档。有趣的是,在Safari中,代码进入上面的 if(e.preventDefault)部分但由于某种原因它不会阻止默认操作,即刷新页面。

The above code works perfectly for IE 6,7 and 8, from Firefox 2.0 onwards and also in Chrome. However it does not work for Safari 4 for windows. Pressing the F5 key refreshes the document. 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 i.e. refreshing the page.

这是Safari 4的错误还是我需要写一些特定于Safari的代码?

Is this a bug with Safari 4 or is there some safari specific code i need to write?

谢谢

推荐答案

不要尝试任何妨碍用户使用浏览器正常操作的内容。即使您使用javascript执行此操作,他也可以禁用该脚本,然后继续页面刷新。

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.

如果您需要阻止在刷新时执行的操作,请在服务器中处理它一边。

If you need to prevent the action made on a refresh then handle it in server side.

这篇关于在Safari 4中禁用F5键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆