如何通过"onbeforeunload"禁用警告.当“重新加载"时同一页? [英] How to disable Warning by "onbeforeunload" when "reload" same page?

查看:68
本文介绍了如何通过"onbeforeunload"禁用警告.当“重新加载"时同一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码运行良好,但是当reloadind 相同页面(重新加载按钮或F5键)或点击后退"时,我不需要此"beforeunload警告" 按钮...

My code works well, but I do not need this "beforeunload warning" when reloadind same page (reload button ou F5 key) , or when a click in the "back" button...

我原来的工作代码:

<script>
    window.onbeforeunload = function (e) {
        var msg = '\n\n\nARE YOU SURE?\n\n\n';
        e = e || window.event;

        if (e)
            e.returnValue = msg;
        //some extra conditions
        document.getElementById("popUpOut").style.display = 'block';
        return msg;
    }
</script>

所以,这是我的问题:在这种情况下(后退按钮"和重新加载页面")如何禁用beforeunload?

So, this is my question: How to disable beforeunload in these situations ("back button" and "reload page)?

推荐答案

您不能这样做.页面刷新就像导航并卸载DOM一样,因此将始终调用onbeforeunload事件,但是您可以使用jquery按住Ctrl + R或F5和Backspace的键来阻止它.

You can't do that. A page refresh is like navigating away and unloading the DOM so the onbeforeunload event will always be called but you can prevent it using jquery for keys pressed for Ctrl + R or F5 and Backspace.

对于Ctrl + R,请使用以下命令:

For Ctrl + R use this:

$(document).keydown(function (e) {
    if (e.keyCode == 65 && e.ctrlKey) {
        e.preventDefault();
    }
});

对于F5使用此:

$(document).keydown(function (e) {
    if (e.which || e.keyCode) == 116) {
        e.preventDefault();
    }
});

这篇关于如何通过"onbeforeunload"禁用警告.当“重新加载"时同一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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