有一种方法来防止刷新鼠标点击浏览器

Is there a way to prevent refresh on mouse click of a browser?

本文关键字:刷新 鼠标 浏览器 方法 一种      更新时间:2023-09-26

我通过清除历史记录和刷新页面的可能关键事件来阻止浏览器后退按钮,但我唯一需要的是防止鼠标单击浏览器的刷新按钮。有人能帮我一下吗?

history.pushState("", "");
window.addEventListener('popstate', function(event) {
    history.pushState("", "");
});
$(function() {
    document.onkeydown = function(event) {
        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;
                }
        }
    }
});

考虑window.onbeforeunload事件处理程序。在您的页面中,使用隐藏字段来控制何时离开(刷新-是离开并再次进入)您的页面是合法的。

<script type="text/javascript">
    window.onbeforeunload = function() {
        var isPageUnloadAllowed = true;
        // check legitimacy of page unload, and return false, if it is prohibited, if it is allowed, return true 

        return isPageUnloadAllowed;
    }
</script>