如何防止用户在Mozila Firefox 24中使用javascript或jquery按F5和CTRL+R

How to prevent user to press F5 ang CTRL+R in Mozila Firefox 24 using javascript or jquery

本文关键字:jquery javascript F5 CTRL+R 用户 何防止 Mozila Firefox      更新时间:2024-07-05

如何防止用户在Mozilla Firefox 24中使用javascript或jquery按下F5CTRL+1R

你不能真正阻止用户重新加载你的页面,但你可以在离开或重新加载之前显示一条消息:

window.onbeforeunload = function(e) {
    return 'Do You really want to leave the page?';
};

您也可以为Ctrl或F5键设置事件处理程序,并防止发生默认情况,但出于安全原因,它在大多数浏览器中不适用于功能键(如Ctrl或F键)。

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